In this article we are going to discuss about param keyword with examples in C#.
What is params keyword? When to use params keyword in real applications?
The params keyword allows you to pass a variable number of arguments to a method. It can only be used with a single parameter, and it must be the last parameter in the method signature. In real applications, you use params when you do not know the exact number of arguments that will be passed to the method.
Example
public void PrintNumbers(params int[] numbers)
{
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
static void Main()
{
PrintNumbers(1, 2, 3, 4, 5); // Output: 1 2 3 4 5
}