In this article we have explained params parameter with an example in c-sharp code and we have also stated three restrictions in declaring params parameter.
What is Params Parameter?
We use params keywords to pass variable number of arguments to a method or function.
We can send comma separated list of arguments to a method.
During compilation with params, the arguments passes to a method are changed into elements in a temporary array. This temporary array can be further used in a method code to get values.
Syntax of Params Parameter
1 | public int Multiplication( params int [] values){ |
In a method or a function of round braces we can use "params" keyword followed by data type with array like square brackets then "params" keyword name.
Step by Step Params parameter with an example in C#
Let's do a small example using "params" keyword in a console application using C#.
Step 1 : Create Console Application in C#
First open up visual studio and create a new console application project using languague c#.
03 | static void Main( string [] args) |
Step 2 : Create a Method
Let's create a static method in main console application program with return types as"int" and input argument as "params" keyword with "int[]" data type as shown in below snippet of code.
03 | static void Main( string [] args) |
10 | static int Addition( params int [] values) |
Now in a method scope of an
"Addition" let's write some code which will do addition of all input values. Since input value is in an array
"integer" type format, we need to use
FOR-Each Loop to fetch integer values from an
array.
03 | static void Main( string [] args) |
10 | static int Addition( params int [] values) |
13 | foreach ( int value in values) |
As you in an above code that for-each loop takes each value from array and adds to an new "integer" variable "answer".
Step 3 : Calling Static Method
In this step we will call "Addition" method in our main program and pass some input values.
03 | static void Main( string [] args) |
06 | int add1 = Addition(1); |
07 | int add2 = Addition(2, 2, 2, 2); |
08 | int [] addarray = new int [3] { 3, 3, 3 }; |
09 | int add3 = Addition(addarray); |
13 | static int Addition( params int [] values) |
16 | foreach ( int value in values) |
As you can see from above code that we have called our static method "Addition" in our main method and to this method we have passed input values in different ways as shown below.
1st way - Only single value.
2nd way � Multiple values separated by comma.
3rd way - passing an array.
1 | static void Main( string [] args) |
4 | int add1 = Addition(1); |
5 | int add2 = Addition(2, 2, 2, 2); |
6 | int [] addarray = new int [3] { 3, 3, 3 }; |
7 | int add3 = Addition(addarray); |
Step 4 : Displaying Output
Conclusion
1st way - Addition of 1 = 1.
2nd way - Addition of 2 + 2 + 2 + 2 = 8.
3rd way - Addition of 3 + 3 + 3 = 9 using array.
So as you see we have successfully executed the program and we use params keywords to pass variable number of arguments to a method or function.
Complete Code
03 | static void Main( string [] args) |
05 | int add1 = Addition(1); |
06 | int add2 = Addition(2, 2, 2, 2); |
07 | int [] addarray = new int [3] { 3, 3, 3 }; |
08 | int add3 = Addition(addarray); |
09 | Console.WriteLine( "1st Way Output == {0}" ,add1); |
10 | Console.WriteLine( "2nd Way Output == {0}" , add2); |
11 | Console.WriteLine( "3rd Way Output == {0}" , add3); |
12 | Console.WriteLine( "\n" ); |
14 | string [] animalnames = new string [5] { "Cow" , "Goat" , "Tiger" , "Monkey" , "Lion" }; |
15 | DisplayNames(animalnames); |
16 | DisplayObject( "OnlineBuff" , 009, 99.9, "Mumbai" ); |
21 | static int Addition( params int [] values) |
24 | foreach ( int value in values) |
31 | static void DisplayNames( params string [] values) |
33 | for ( int i = 0; i < values.Length; i++) |
35 | Console.Write(values[i] + "\n" ); |
40 | static void DisplayObject( params object [] values) |
42 | for ( int i = 0; i < values.Length; i++) |
44 | Console.Write(values[i] + "\n" ); |
Copy&Paste above code in your console application and run the program to see the final output.
Three Restriction of using Params keyword in c#
We can send only comma separated list of arguments to a method.
No additional parameters are permitted after the params keyword in a method declaration.
Only one params keyword is permitted in a method declaration.
No comments:
Post a Comment