Sunday, 15 June 2014

Params Parameter in C#

Understand params parameter with an example in C#?

 
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

1public int Multiplication(params int[] values){
2 
3///Code goes here
4 
5}
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#.
01class Program
02{
03        static void Main(string[] args)
04        {
05 
06            ///code
07 
08        }
09 
10}

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.
01class Program
02{
03        static void Main(string[] args)
04        {
05 
06            ///code goes here
07 
08        }
09 
10       static int Addition(params int[] values)
11       {
12            //Code goes here
13 
14       }  
15 
16}
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 useFOR-Each Loop to fetch integer values from an array.
01class Program
02{
03        static void Main(string[] args)
04        {
05 
06            ///code
07 
08        }
09 
10      static int Addition(params int[] values)
11      {
12            int answer = 0;
13            foreach (int value in values)
14            {
15                answer += value;
16            }
17            return answer;
18 
19      }  
20 
21}
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.
01class Program
02{
03        static void Main(string[] args)
04        {
05 
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);
10 
11        }
12 
13      static int Addition(params int[] values)
14      {
15            int answer = 0;
16            foreach (int value in values)
17            {
18                answer += value;
19            }
20            return answer;
21 
22      }  
23 
24}
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.
1static void Main(string[] args)
2{
3 
4   int add1 = Addition(1); //Only single value.
5   int add2 = Addition(2, 2, 2, 2); //Multiple values separated by comma.
6   int[] addarray = new int[3] { 3, 3, 3 };
7   int add3 = Addition(addarray); //passing an array.
8 
9}

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
01class Program
02{
03    static void Main(string[] args)
04    {
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");
13 
14        string[] animalnames = new string[5] { "Cow""Goat""Tiger""Monkey","Lion" };
15        DisplayNames(animalnames);
16        DisplayObject("OnlineBuff", 009, 99.9, "Mumbai");
17         
18 
19    }
20 
21    static int Addition(params int[] values)
22    {
23        int answer = 0;
24        foreach (int value in values)
25        {
26            answer += value;
27        }
28        return answer;
29    }
30 
31    static void DisplayNames(params string[] values)
32    {
33        for (int i = 0; i < values.Length; i++)
34        {
35            Console.Write(values[i] + "\n");
36        }
37        Console.WriteLine();
38    }
39 
40    static void DisplayObject(params object[] values)
41    {
42        for (int i = 0; i < values.Length; i++)
43        {
44            Console.Write(values[i] + "\n");
45        }
46        Console.WriteLine();
47    }
48 
49}
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