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.
Following is an example of using params
keyword in c# programming language to specify method parameters accept the multiple numbers of arguments.
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.
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.
Following is an example of using object
type parameter in a method declaration to accept the list of multiple types of arguments.
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.
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.