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.
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.
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.
Following is the example of sending a single dimensional array to the method in the c# programming language.
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.
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.
Following is the example of sending a multi-dimensional array to the method in the c# programming language.
If you observe the above example, we created a multi-dimensional array called “arr” and sent it as an argument to the GetDetails method.
When you execute the above c# program, you will get the result below.
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.