Params Parameter in C# with example

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

 

public int Multiplication(params int[] values){

///Code goes here

}

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#.

 

class Program
{
        static void Main(string[] args)
        {

            ///code

        }

}

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.

 

class Program
{
        static void Main(string[] args)
        {

            ///code goes here

        }

	   static int Addition(params int[] values)
	   {
			//Code goes here

	   }   

}

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.

 

class Program
{
        static void Main(string[] args)
        {

            ///code

        }

	  static int Addition(params int[] values)
	  {
            int answer = 0;
            foreach (int value in values)
            {
                answer += value;
            }
            return answer;

	  }   

}

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.

 

class Program
{
        static void Main(string[] args)
        {

           int add1 = Addition(1);
           int add2 = Addition(2, 2, 2, 2);
           int[] addarray = new int[3] { 3, 3, 3 };
           int add3 = Addition(addarray);

        }

	  static int Addition(params int[] values)
	  {
            int answer = 0;
            foreach (int value in values)
            {
                answer += value;
            }
            return answer;

	  }   

}

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.

 

        static void Main(string[] args)
        {

           int add1 = Addition(1); //Only single value.
           int add2 = Addition(2, 2, 2, 2); //Multiple values separated by comma.
           int[] addarray = new int[3] { 3, 3, 3 };
           int add3 = Addition(addarray); //passing an array.

        }


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
 


    class Program
    {
        static void Main(string[] args)
        {
            int add1 = Addition(1);
            int add2 = Addition(2, 2, 2, 2);
            int[] addarray = new int[3] { 3, 3, 3 };
            int add3 = Addition(addarray);
            Console.WriteLine("1st Way Output == {0}",add1);
            Console.WriteLine("2nd Way Output == {0}", add2);
            Console.WriteLine("3rd Way Output == {0}", add3);
            Console.WriteLine("\n");

            string[] animalnames = new string[5] { "Cow", "Goat", "Tiger", "Monkey", "Lion" };
            DisplayNames(animalnames);
            DisplayObject("OnlineBuff", 009, 99.9, "Mumbai");
            

        }

        static int Addition(params int[] values)
        {
            int answer = 0;
            foreach (int value in values)
            {
                answer += value;
            }
            return answer;
        }

        static void DisplayNames(params string[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                Console.Write(values[i] + "\n");
            }
            Console.WriteLine();
        }

        static void DisplayObject(params object[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                Console.Write(values[i] + "\n");
            }
            Console.WriteLine();
        }

    }


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.

Hey friend..!This is about params keyword in c-sharp if you have any doubts in using params keyword please feel free to ask me or if you want to give me feed-back you are most welcome. Your valuable feed-back will help me to grow. Kindly share this post on your social channels - Google+, Facebook and twitter. Post your Feed-Back and Doubts in the comment section below.

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more

Comments

64x64
By V.sravani on 2015-02-06
The solution for the params is very nice to understand

Add a Comment