
In C# programming, the "params" keyword plays a crucial role in enhancing flexibility and convenience when dealing with methods that accept a variable number of the same type of parameters.
C# Param arguments
This feature is particularly useful when the exact number of arguments isn't known beforehand, allowing developers to pass an arbitrary number of parameters or optional parameters of the same type in a method declaration.
class Program
{
public static void AddItemsToShoppingBasket(params string[] items)
{
// ....
}
public static void AddItemsSumToShoppingBasket(params int[] sum)
{
// ....
}
}
Here, we do not know the count of items beforehand, and the method is written in such a way by specifying the params keyword that it accepts string items without specifying the count.
The params keyword is employed in this method signature declarations to denote that a parameter array can be passed to the method. This array can hold zero or more values of a specified type and can also be an optional parameter/hold optional arguments.
The default value for this parameter is an empty array of the type specified in the method signature. For example, when a method containing the argument 'params object[] objects' is called without any parameters, it creates an empty array of objects.
As shown below, "AddToShoppingBasket" can be invoked with a variable number of arguments of string parameters. The params keyword simplifies the syntax for the method call by allowing developers to pass the optional parameters directly without explicitly creating an array.
class Program
{
AddItemsToShoppingBasket("cake", "pizza", "cold drink");
AddItemsToShoppingBasket("snacks", "burger");
AddItemsToShoppingBasket(); // Valid with zero parameters
}
Considerations and Best Practices
Now that we have introduced the params keyword in general let us dive deep into some considerations and best practices of the 'params' keyword.
The parameter type must be a one-dimensional array.
To use the params keyword specified in the method "AddItemsToShoppingBasket" defined above, we can pass a one-dimensional array instead of manually calling the method using parameters like below.
// example
class Program
{
var items = [] {"cake", "pizza", "cold drink"}; // one-dimensional string array
AddItemsToShoppingBasket(items); // works
AddItemsToShoppingBasket("cake", "pizza", "cold drink"); // same as above line
}
Can pass a comma-separated list of arguments for the type of the array elements.
While using a method with the params keyword, we can pass multiple values of arguments as comma-separated values.
// example method signature
class Program
{
AddItemsToShoppingBasket("snacks", "burger", "snacks", "burger", "cold drink"); // comma separated values
AddItemsToShoppingBasket("snacks");
}
Pass an array of arguments of the correct type
The below code throws an error if we try to add items of type int into the shopping basket.
// example
AddItemsToShoppingBasket("snacks",2,"burger"); // error
AddItemsToShoppingBasket(2,3,4); // error as params type is string


Join the conversation! Your thoughts help the community grow.