Passing Arrays As Arguments in C# PDF
Passing Arrays As Arguments in C# PDF
Courses Login
Suggest an Article
perm_identity
Passing arrays as arguments in C#
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.
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 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.
Example 2: Declaring, initializing and passing the array to the method in a single line of
code.
class GFG {
// declaring a method
static void Result(int[] arr) {
// Main method
public static void Main() {
// declaring an array
// and intializing it
int[] arr = {1, 2, 3, 4, 5};
Array Element: 1
Array Element: 2
Array Element: 3
Array Element: 4
Array Element: 5
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.
Example 2: Declaring, initializing and passing the 2-D array to the method in a single line
of code.
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.
Recommended Posts:
C# | Command Line Arguments
C# | Arrays
C# | Jagged Arrays
C# | Arrays of Strings
C# | Using foreach loop in arrays
C# | Implicitly Typed Arrays
How to create the StringBuilder in C#
Object.ReferenceEquals() Method in C#
C# | Int 64 Struct
Int32.MaxValue Field in C# with Examples
Int16.MaxValue Field in C# with Examples
Int16.MinValue Field in C# with Examples
C# | Int32 Struct
Int64.MaxValue Field in C# with Examples
Anshul_Aggarwal
Believes in Smart Work
If you like GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to [email protected]. See
your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve
Article" button below.
thumb_up
Be the First to upvote.
To-do Done 0
No votes yet.
Please write to us at [email protected] to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials
PRACTICE CONTRIBUTE
Company-wise Write an Article
Topic-wise Write Interview Experience
Contests Internships
Subjective Questions Videos