Click here to Skip to main content
15,896,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

firs time posting. I'm a C# noob, and still learning.

I am trying to write a simple console app that will initialize an array, then pass those values to a method which will then multiply the value by 5 and output the result to the console....
I can't seem to get this right any help will be appreciated, below is what I have so far but I get an exception error telling me the index is out of range??

C#
namespace ConsoleApplication5
{
    class Program
    {

        static void mx(int[] numbers)
        {
            foreach (int i in numbers)
            {
                numbers[i] = numbers[i]*5;

                Console.WriteLine(numbers[i]);
            }
        }

        static void Main()
        {
            int[] numbers = new int[10] {1,2,3,4,5,6,7,8,9,10};

            Program.mx(numbers);

            Console.ReadLine();

        }




    }
}
Posted

C# array starts from 0 and you are trying to iterate outside the limit/size of the defined array with this code -> numbers[i] = numbers[i]*5;
So you get an index out of range exception.

Modify as below


C#
class Program
       {

           static void mx(int[] numbers)
           {
               foreach (int i in numbers)
               {
                  // numbers[i] = numbers[i] * 5;

                   Console.WriteLine(i * 5);
               }
           }

           static void Main()
           {
               int[] numbers = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

               Program.mx(numbers);

               Console.ReadLine();

           }




       }
 
Share this answer
 
v2
You use a foreach loop. This enumerates the elementes inside the array. Then you use the number as index. Last number in your array is 10, this is invalid index because lower bound of the array is 0 and upper bound is 9. Mathew's solution is fine as long as you don't care for updating the original array. Here is an alternative which does that:

C#
for (int i=0; i < numbers.Length; i++)
{
  numbers[i] = numbers[i]*5; 
  Console.WriteLine(numbers[i]);
}
 
Share this answer
 
This example uses Lambda notation to assign an Action Delegate that the iterator Function (ForEach) applies to each element in the List it iterates over:
C#
// requires System.Collections.Generic
// does not require Linq
// 'Collection.ForEach available as of .NET 2.0, will not work with an Array
static void Main()
{
    new List<int>{1,2,3,4,5,6,7,8,9,10}.ForEach(i => Console.WriteLine(i * 5));

    Console.ReadLine();
}</int>
If this example confuses you, let me know, and I'll remove it.
 
Share this answer
 
v2
Hi Guys

Thanks so much for the help you all have been great and I learned a few new ways to do things. I did manage to figure out that I shouldn't be using a foreach loop and had changed the code as below:

I know it's still not "correct" or best practice... but I'm learning. thanks again all.

C#
namespace ConsoleApplication5
{
    class Program
    {

        static void mx(int[] numbers)
        {
            for (int i = 0; i < numbers.Length; i++)
            {
                int num = numbers[i];
                num = num * 5;
                Console.WriteLine(num);
            }
        }

        static void Main()
        {
            int[] numbers = new int[10] {1,2,3,4,5,6,7,8,9,10};

            Program.mx(numbers);

            Console.ReadLine();

        }




    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900