C# Passing Arrays as Arguments

  Keywords : C# passing arrays as function or method parameter with examples, C# pass single or multidimensional arrays as parameters with examples

Here we will learn how to pass c# arrays as function or method arguments / parameters with examples and how to pass c# single dimensional and multidimensional arrays as arguments to methods with examples.

C# Passing Arrays as Parameters / Arguments

In previous chapters, we learned what are 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 c# programming language.

 

In c#, arrays are the reference types so we can pass arrays as an arguments to the method parameters and we can modify the values of an array elements inside of 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 sends an array to the UserDetails method.

 

// Single Dimensional Array

string[] names = { "suresh", "rohini", "trishika", "praveen" };

UserDetails(names);

 

// Method to accept array as parameter

public static void UserDetails(string[] arr)

{

// your custom code

}

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

 

Now we will see the complete example of sending a single dimensional array to the method in 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 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we run above c# program, we will get the result like as shown below.

 

C# Pass Single Dimensional Array to Method Example Result

 

If you observe above result, methods are able accept 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 c# programming language.

C# Pass Multidimensional Arrays to Method Example

Following is the example of sending a multi-dimensional array to the method in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we run above c# program, we will get the result like as shown below.

 

C# Pass Multidimensional Arrays to Method Example Result

 

If you observe above result, methods are able accept 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 c# programming language based on our requirements.