C Sharp Params

C# Params

To specify a parameter to take a variable number of arguments, especially when the number of arguments is not priorly known, the params keyword is used in C#. In C#, only one params keyword is allowed in a function declaration. After the params keyword, no other parameter is permitted.

Example 1:

using System;  
namespace param_example  
{  
    class Example  
    {  
        public void Display(params int[] a) 
        {  
            for (int i=0; i<a.Length; i++)  
            {  
                Console.WriteLine(a[i]);  
            }  
        }  
 
        static void Main(string[] args)  
        {   //Creating Object 
            Example xyz = new Example(); 
            //Passing arguments of variable length   
            xyz.Display(9,7,5,3,1); 
        }  
    }  
}

Output:

Explanation:

In the above example, we are displaying the use and working of the C# params.

Example 2:

using System;  
namespace param_example  
{  
    class Example  
    {  
        public void Display(params object[] a) // Params Paramater  
        {  
            for (int i = 0; i < a.Length; i++)  
            {  
                Console.WriteLine(a[i]);  
            }     
        }  
        static void Main(string[] args)  
        {   // Creating Object 
            Example xyz = new Example();  
            // Passing arguments of variable length  
            xyz.Display("A”, "B", "C", 'D', 1, 9.9); 
        }     
    }  
}

Output:

Explanation:

In the above example, we are allowing the entry of any number of inputs of any type by using object type params.