C# Params Keyword with Examples

In c#, params keyword is useful to specify a method parameter that takes a variable number of arguments. The params keyword is useful when we are not sure about the 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 arguments of the specified type as a comma-separated list or an array to the declared parameter. 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 an example of using params keyword in c# programming language to specify method parameters accept the multiple numbers of arguments.

 

using System;

namespace Tutlane
{
     class Program
     {
         static void Main(string[] args)
         {
             ParamsMethod(1, 2, 3, 4, 5, 6);
         }
         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 the above example, we send a comma-separated list of multiple arguments of the specified type (integer) to the declared parameter in the ParamsMethod function.

 

When you execute the above c# program, you will get the result below.

 

C# Params Keyword Example Result

 

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

 

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

C# Params Keyword with Object Type

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

 

using System;

namespace Tutlane
{
     class Program
     {
         static void Main(string[] args)
         {
             ParamsMethod(1, 2, "suresh", "rohini", "trishika", 10.26);
         }
         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 the above example, we used object type parameter in method declaration and accepting different data types of arguments as a list.

 

When you execute the above c# program, you will get the result below.

 

C# Params Keyword with Object Type Example Result

 

If you observe the above result, we are able to send different data types of arguments to the params keyword parameter in the 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.