C# Params Keyword
Syntax
public void FunctionName(params dataType[] args)
Example
public void PrintNames(params string[] names)
{
Console.WriteLine("List of Names:");
foreach (string name in names)
{
Console.WriteLine(name);
}
}
Output
The output may vary depending on the arguments passed to the function.
Explanation
In C#, params
is a keyword used to specify a parameter array. It allows for a variable number of arguments to be passed to a function. The parameter array must be the last parameter in the function signature. Only one parameter array is allowed per function.
The params
keyword is useful when we do not know the number of arguments that will be passed to a function at compile time. The parameter array is treated like any other array.
Use
The params
keyword is used to specify a parameter array in C#. It allows us to pass a variable number of arguments to a function. This is useful when we do not know the number of arguments that will be passed to the function at compile time. We can use this keyword to create more flexible functions that can handle a wide range of inputs.
Important Points
- The
params
keyword is used to specify a parameter array in C#. - It allows for a variable number of arguments to be passed to a function.
- The parameter array must be the last parameter in the function signature.
- Only one parameter array is allowed per function.
- The parameter array is treated like any other array.
Summary
The params
keyword in C# is used to specify a parameter array. It allows us to pass a variable number of arguments to a function. This keyword is useful when we do not know the number of arguments that will be passed to the function at compile time. The parameter array is treated like any other array and is a great way to create more flexible functions.