An array is a collection of similar type variables which are referred to by a common name. In C#, arrays are the reference types so it can be passed as arguments to the method. A method can modify the value of the elements of the array. Both single-dimensional and multidimensional arrays can be passed as an argument to the methods.
Passing 1-D Arrays as arguments to methods
One can pass the 1-D arrays to a method. There are various options like first, you declare and initialize the array separately then pass it to the method. Second, you can declare, initialize and pass the array to the method in a single line of code.
Example 1: Declaring and initializing array first and then pass it to the method as an argument.
// taking an integer array // declaring and initializing // the array int [] arr = {1, 2, 3, 4};
// passing the array as an // argument to the method // Result is the method name Result(arr); |
Example 2: Declaring, initializing, and passing the array to the method in a single line of code.
Result(new int[] {1, 2, 3, 4});
Code: In the below program, we are passing the 1-D array arr to the method Result. The method is static and it will print the array elements which are passed to it.
// C# program for passing the 1-D // array to method as argument using System;
class GFG {
// declaring a method
static void Result( int [] arr) {
// displaying the array elements
for ( int i = 0; i < arr.Length; i++)
{
Console.WriteLine( "Array Element: " +arr[i]);
}
}
// Main method
public static void Main() {
// declaring an array
// and initializing it
int [] arr = {1, 2, 3, 4, 5};
// calling the method
Result(arr);
}
} |
Output:
Array Element: 1 Array Element: 2 Array Element: 3 Array Element: 4 Array Element: 5
Passing Multi-Dimensional Arrays as arguments to Methods
You can also pass the multidimensional arrays to a method. There are various options like first, you declare and initialize the multi-dimensional array separately then pass it the to the method. Second, you can declare, initialize and pass the array to the method in a single line of code.
Example 1: Declaring and initializing array first and then pass it to the method as an argument.
// declaring and initializing // the 2-D array int [,] arr = { {1, 2, 3, 4}, {5, 6, 7, 8} };
// passing the array as an // argument to the method // Result is the method name Result(arr); |
Example 2: Declaring, initializing and passing the 2-D array to the method in a single line of code.
Result(new int[,] { {1, 2, 3, 4}, {5, 6, 7, 8} });
Code: In the below program, we are passing an 2-D array arr to the method transpose which gave the transpose of the matrix. GetLength() method is used for the count the total number of elements in a particular dimension.
// C# program for finding the transpose // of matrix(2-D array) by using array // as function arguments using System;
class GFG {
// temp is used as temporary variable
static int temp = 0;
// passing 2-D array 'arr' as argument
// to find the transpose of matrix
static void transpose( int [, ] arr)
{
for ( int i = 0; i < arr.GetLength(0); i++) {
for ( int j = i; j < arr.GetLength(1); j++) {
temp = arr[i, j];
arr[i, j] = arr[j, i];
arr[j, i] = temp;
}
}
}
// to display the transposed matrix
static void displayresult( int [, ] arr)
{
Console.WriteLine( "Matrix After Transpose: " );
for ( int i = 0; i < arr.GetLength(0); i++) {
for ( int j = 0; j < arr.GetLength(1); j++)
Console.Write(arr[i, j] + " " );
Console.WriteLine();
}
}
// Main Method
static public void Main()
{
// declaration of an 2-d array
int [, ] arr;
// initializing 2-D array
// matrix of 4 rows and 4 columns
arr = new int [4, 4]{ { 1, 2, 3, 4},
{ 5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
Console.WriteLine( "Matrix Before Transpose: " );
for ( int i = 0; i < arr.GetLength(0); i++)
{
for ( int j = 0; j < arr.GetLength(1); j++)
Console.Write(arr[i, j] + " " );
Console.WriteLine();
}
Console.WriteLine();
// calling transpose method
transpose(arr);
// calling displayresult method
// to display the result
displayresult(arr);
}
} |
Output:
Matrix Before Transpose: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Matrix After Transpose: 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16