C# Params Keyword with Examples

In c#, params keyword is used to specify a method parameter that takes a variable number of arguments. The params keyword is useful when we are not sure about number of arguments to send as a parameter.

 

In c#, during method declaration only one params keyword is allowed and no additional parameters are permitted after the params keyword in a method declaration.

 

We can send an arguments of specified type as a comma-separated list or an array to the declared parameter. In case, if we are not sending any arguments to the defined parameter, then the length of params list will become a zero.

C# Params Keyword Example

Following is the example of using params keyword in c# programming language to specify method parameter accept multiple number of arguments.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };

            ParamsMethod(arr);

        }

        public static void ParamsMethod(params int[] arr)

        {

            for (int i = 0; i < arr.Length; i++)

            {

                Console.Write(arr[i] + (i < arr.Length - 1 ? ", " : ""));

            }

            Console.WriteLine();

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we are sending a comma separated list of multiple arguments of the specified type (integer) to the declared parameter in ParamsMethod function.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Params Keyword Example Result

 

If you observe above result, we are able to send multiple arguments of same data type to params keyword parameter in method declaration.

 

In previous example, we are sending only integer type of arguments to the method parameter. In case, if you want to send a list of multiple type of arguments, then we need to use object type parameter in method declaration.

C# Params Keyword with Object Type

Following is the example of using object type parameter in method declaration to accept list of multiple type of arguments.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            object[] arr =  { 1, 2, "suresh", "rohini", "trishika", 10.26 };

            ParamsMethod(arr);

        }

        public static void ParamsMethod(params object[] arr)

        {

            for (int i = 0; i < arr.Length; i++)

            {

                Console.Write(arr[i] + (i < arr.Length - 1 ? ", " : ""));

            }

            Console.WriteLine();

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used object type parameter in method declaration and accepting different data type of arguments as a list.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Params Keyword with Object Type Example Result

 

If you observe above result, we are able to send different data type of arguments to the params keyword parameter in method declaration.

 

This is how we can params keyword in c# programming language to send a list of arguments to the parameter in method declaration based on our requirements.

Topics Covered