Arrays as Arguments

[print-me]

You can pass arrays as arguments to a method. You have to specify the parameters of the method that it will accept an array. Let’s take a look at an example.

using System;

namespace ArraysAsArgumentsDemo1
{
    public class Program
    {
        static void TestArray(int[] numbers)
        {
            foreach (int number in numbers)
            {
                Console.WriteLine(number);
            }
        }

        public static void Main()
        {
            int[] array = { 1, 2, 3, 4, 5 };

            TestArray(array);
        }
    }
}

Example 1 – Passing Arrays as Arguments

1
2
3
4
5

We simply put square brackets after the data type of the parameter of the  method to indicate that the parameter is expecting an array. When we call the method  in line 19, we just pass the array using its name without the index. This will pass the address of the array to the method. When we used the foreach loop(line 9-12), we access each of the elements of the original array that was passed as the argument to the method. The next program below demonstrates that we are passing the array by reference.

using System;

namespace ArraysAsArgumentsDemo2
{
    public class Program
    {
        static void IncrementElements(int[] numbers)
        {
            for (int i = 0; i < numbers.Length; i++)
            {
                numbers[i]++;
            }
        }

        public static void Main()
        {
            int[] array = { 1, 2, 3, 4, 5 };

            IncrementElements(array);

            foreach (int num in array)
            {
                Console.WriteLine(num);
            }
        }
    }
}

Example 2 – Array Arguments Are Passed by Reference

2
3
4
5
6

The program above presented a method that accepts an array and then increment each of its element. Note that we didn’t use a foreachloop to increment the values of each of the element because foreach  loops are only suitable for reading each element and not modifying them. Inside  the method, we increment the value of each element of the array. After exiting the method, we displayed the results and as you can see, the original values of  the method are also modified.

Another way of passing an array to a method is by giving the values directly in the method call. For example:

IncrementElements( new int[] { 1, 2, 3, 4, 5 } );

We didn’t declare an array. Instead, we pass a set of values to the parameter that is expecting an array. Since there was no array variable that was declared, we cannot use it in the Main method for displaying the results.

If there are multiple parameters in a method, always place square brackets for each of the parameters the will expect n array. For example:

void MyMethod(int[] param1, int param2)
{
   //code here
}

Looking at the header, param1 is expecting an array argument, but param2 is accepting a normal int number. You must put square brackets if param2 should also accept an array.

void MyMethod(int[] param1, int[] param2)
{
   //code here
}