C# Passing An Array To A Function

In the C# programming language, arrays are a collection of elements of the same data type that are in a single variable . If we need to reuse array-related logic, we can define a method and pass the array to it as a parameter. We only need to pass the array's name (without brackets) when calling the method, and it will be passed by reference.

Syntax

It has the following syntax:

Example

function_name(array_name);  // passing array to the method

In this syntax,

  • function_name: It represents the name of the method.
  • array_name: It represents the array being passed as an argument.
  • Note: An array is a reference type. It means when we pass an array to a function, we are actually passing a link to the original array, not a new copy. Therefore, any modification of an array inside the function will affect the original array.

    Passing 1-D Arrays as arguments to the methods

In the C# programming language , there are several ways to pass one-dimensional arrays as arguments to a method.

  • The array can be declared and initialized the before passing it to the method.
  • The array can be declared, initialized, and passed to the method all in a single line of code.

Syntax

It has the following syntax:

Example

void Method_name(int[] arr)

{

    // Access or modify array elements

}

Passing an Array to a Function Example in C#

Let us take an example to illustrate how we can pass one-dimensional arrays as arguments to methods.

Example

Example

using System;

class Program	

{

    // Declare Method that takes a 1D array as a parameter

static void PrintArray(int[] num_bers)

    {

Console.WriteLine("Array elements:");

foreach (int num in num_bers)

        {

Console.WriteLine(num);

        }

    }



static void Main()

    {

        // declaration and initialization of a 1-D array

int[] my_array = { 10, 20, 30, 40, 50 };

        // Passing to the array in the method

PrintArray(my_array);

    }

}

Output:

Output

Array elements:

10

20

30

40

50

Explanation:

In this example, we demonstrate how to pass a 1-D array as arguments to a method. First, we create a method named PrintArray that takes a multidimensional array as a parameter. After that, we use the foreach loop to print each element. In the main function, we declare an integer type array and assign a value. These arrays are passed to the PrintArray method, and then the program shows the output.

Passing multidimensional arrays as arguments to Methods

In C# programming, we can also pass multidimensional arrays to a method. We can declare and initialized the array separately before passing it, or it can be declared, initialized, and passed to the method all in a single line of code.

Syntax

It has the following syntax:

Example

returnType MethodName(dataType[], arrayName)

{

    // code to be executed

}

Passing Multidimensional arrays as arguments Example in C#

Let us take an example to illustrate how we can pass the multidimensional arrays as arguments to methods.

Example

Example

using System;

class Program

{

    // Create a Method 

static void ShowNumbers(int[,] numbers)

    {

        // Iterate over each rows and columns

for (int i = 0; i< 2; i++)  // 2 rows

        {

for (int j = 0; j < 3; j++)  // 3 columns

            {

Console.Write(numbers[i, j] + " ");

            }

Console.WriteLine(); // New line after each row

        }

    }

static void Main()

    {

        // Declaration and Initialization of 2D array

int[,] myNumbers = {

{ 1, 2, 3 },

{ 4, 5, 6 }

        };

        // Pass the 2D array 

ShowNumbers(myNumbers);

    }

}

Output:

Output

1 2 3 

4 5 6

Explanation:

In this example, we demonstrate how to pass a multidimensional array as arguments to a method. First, we create a method named ShowNumbers that takes a dimensional array as a parameter. After that, we use the nested for loop to print each element. In the main function, we declare an integer type array and assign the values. These arrays are passed to the ShowNumbers method, and then the program shows the output.

C# Passing an Array to the function Example: Prints the array elements

Let us take an example to define passing an array to a function in C#.

Example

Example

using System;

public class ArrayExample

{

static void DisplayArr(int[] numbers)

    {

Console.WriteLine("Printing array elements:");

for (int index = 0; index <numbers.Length; index++)

        {

Console.WriteLine(numbers[index]);

        }

    }

public static void Main(string[] args)

    {

int[] data_Set1 = { 100, 200, 300, 400, 500, 600 };

int[] data_Set2 = { 120, 220, 330, 440, 540 };

DisplayArr(data_Set1); // passing array to function

DisplayArr(data_Set2);

    }

}

Output:

Output

Printing array elements:

100

200

300

400

500

600

Printing array elements:

120

220

330

440

540

Explanation:

In this example, we demonstrate how to pass an array to a function and display its elements. First, we create a method named DisplayArr, which takes an array as a parameter. After that, we use a for loop to print each element. In the main method, we create two integer-type variables dataSet1, dataSet2, that are defined with two different values. These arrays are passed to the DisplayArr method one by one, and then the program displays the output.

C# Passing an Array to the function Example: Print the minimum number

Let us take an example to illustrate how we can pass an array of functions that print the minimum number in the array.

Example

Example

using System;

public class ArrayExample

{

static void FindMinimum(int[] numbers)

    {

int smallest = numbers[0];

for (int i = 1; i<numbers.Length; i++)

        {

if (smallest > numbers[i])

            {

smallest = numbers[i];

            }

        }

Console.WriteLine("Minimum element is: " + smallest);

    }

public static void Main(string[] args)

    {

int[] data1 = { 25, 5, 20, 15, 40, 50 };

int[] data2 = { 12, 23, 44, 15, 54 };

FindMinimum(data1); // passing array to function

FindMinimum(data2);

    }

}

Output:

Output

Minimum element is: 5

Minimum element is: 12

Explanation:

In this example, we demonstrate how to pass arrays to a function to find and print the minimum element of an array. First, we create a method named FindMinimum that takes an array as a parameter. After that, we use a for loop to print each element. In the main method, we create an integer type variable data1, and data2 is defined with two different values. These arrays are passed to the FindMinimum method one by one.

C# Passing an Array to a function Example: Print the maximum number

Let us take an example to illustrate how we can pass an array to the function that prints the maximum number.

Example

Example

using System;  

public class ArrayExample

{  

static void printMax(int[] arr)  

    {  

int max = arr[0];  

for (int i = 1; i<arr.Length; i++)  

        {  

if (max <arr[i])  

            {  

max = arr[i];  

            }  

        }  

Console.WriteLine("Maximum element is: " + max);  

    }  

public static void Main(string[] args)  

    {  

int[] arr1 = { 25, 10, 20, 15, 40, 50 };  

int[] arr2 = { 12, 23, 64, 11, 54 };  

printMax(arr1);//passing array to function  

printMax(arr2);  

    }  

}

Output:

Output

Maximum element is: 50

Maximum element is: 64

Explanation:

In this example, we demonstrate how to pass arrays to a function to find and print the maximum element of an array. First, we create a method named printMax that takes an array as a parameter. After that, we use a for loop to print each element. In the main method, we create an integer type variable arr1, and arr2 is defined with two different values. These arrays are passed to the FindMaximum method one by one.

Conclusion

In conclusion, passing arrays to functions enables code reusability and modularity, which makes the programs clear and easy to maintain. C# allows us to pass single or multidimensional arrays to methods either by using reference or parameters, which enables efficient data manipulation and processing within methods.

C# Passing Array to the Function FAQs

1) Can we pass an array to a function in C#?

Yes, we can pass an array to a function in C#.

2) Can we modify the array inside the function in C#?

Yes, we can modify the array inside the function in C#.

3) How do we pass a single-dimensional array to a method?

We can declare and initialize the array and then pass it to the method, or we can declare, initialize and pass the array to the method in a single line of code.

Example

voidOneDArray(int[] arr) {	

for (inti = 0; i<arr.Length; i++) {

Console.Write(arr[i] + " ");

    }

Console.WriteLine();

}

4) Are the arrays passed by reference or by value in C#?

In C#, an array is a reference type. It means when we pass an array to a function, we are actually passing a link to the original array, not a new copy. Therefore, any changes made to the array inside the function will affect the original array.

5) Can we pass an array without declaring it separately in C#?

Yes, we can pass an array directly in C# using an inline declaration.

For Example:

Example

Print_arr(new int[] { 1, 2, 3 } );

Input Required

This code uses input(). Please provide values below: