By using the params keyword, you can specify a method parameter that takes a variable number of arguments.
You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type. You also can send no arguments. If you send no arguments, the length of theparams list is zero.
No additional parameters are permitted after the params keyword in a method declaration, and only one paramskeyword is permitted in a method declaration.
Example:-
Write the below code in a console application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| class Program { static void Main( string [] args) { int y = Add(10, 20); Console.WriteLine(y); Console.ReadLine(); } public static int Add( params int [] MyColl) { int total = 0; foreach ( int i in MyColl) { total += i; } return total; } } |
When we run this code we get the following output.
No comments:
Post a Comment