C# Passing Arrays as Arguments

In previous chapters, we learned the arrays in c#, different types of arrays (single-dimensional, multidimensional and jagged arrays) in c# with examples. Now we will see how to pass arrays as function/method parameters in the c# programming language.

 

In c#, arrays are the reference types to pass arrays as arguments to the method parameters, and we can modify the values of array elements inside the method based on our requirements.

C# Pass Single Dimensional Array as Argument

In c#, if we have an initialized single-dimensional array, then we can pass it to the method as an argument. For example, the following statement will send an array to the UserDetails method.

 

// Single Dimensional Array
string[] names = { "suresh", "rohini", "trishika", "praveen" };
UserDetails(names);

// Method to accept an array as parameter
public static void UserDetails(string[] arr)
{
// your custom code
}

If you observe the above example, we created an array called “names” and sending it as an argument to the method called “UserDetails”.

 

We will see the complete example of sending a single dimensional array to the method in the c# programming language.

C# Pass Single Dimensional Array to Method Example

Following is the example of sending a single dimensional array to the method in the c# programming language.

 

using System;

namespace Tutlane
{
     class Program
     {
        static void Main(string[] args)
        {
            // Single Dimensional Array
            string[] names = { "suresh", "rohini", "trishika", "praveen" };
            UserDetails(names);
        }
        public static void UserDetails(string[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("Element[{0}]: {1}", i, arr[i]);
            }
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
     }
}

If you observe the above example, we created an array called “names” and sending it as an argument to the UserDetails method.

 

When you execute the above c# program, you will get the result as shown below.

 

C# Pass Single Dimensional Array to Method Example Result

 

If you observe the above result, methods are able to accept an array as a parameter to perform required operations based on our requirements.

 

Same way, we can pass multi-dimensional arrays also as a parameter to the methods in the c# programming language.

C# Pass Multidimensional Arrays to Method Example

Following is the example of sending a multi-dimensional array to the method in the c# programming language.

 

using System;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
           // Multi Dimensional Array
           int[,] arr = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
           GetDetails(arr);
       }
       public static void GetDetails(int[,] arr)
       {
           for (int i = 0; i < arr.GetLength(0); i++)
           {
               Console.Write("Element[{0}]: {1}", i,"{");
               for (int j = 0; j < arr.GetLength(1); j++)
               {
                   Console.Write("{0}{1}", arr[i, j], j == (arr.GetLength(1) - 1) ? "" : ",");
               }
               Console.WriteLine("}");
           }
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

If you observe the above example, we created a multi-dimensional array called “arr” and sending it as an argument to the GetDetails method.

 

When you execute the above c# program, you will get the result as shown below.

 

C# Pass Multidimensional Arrays to Method Example Result

 

If you observe the above result, methods are able to accept a multi-dimensional array as a parameter to perform required operations based on our requirements.

 

This is how we can pass arrays as an argument to the method or function in the c# programming language based on our requirements.