The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.
Params parameter is a very useful feature in C#. It is used when we don't know the number of parameters will be passed to the called method.
Param can accept multiple values or "params" should be a single dimensional or a jagged array.
Practical demonstration of passing parameter by param
using System;
namespace param_parameter
{
class Program
{
class XX
{
public void print(params int[] numbers)
{
foreach(int x in numbers)
{
Console.WriteLine(" " + x);
}
}
}
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5, 6 };
int a = 10, b = 20, c = 30, d = 40;
XX obj = new XX();
obj.print(a, b, c, d);
obj.print(numbers);
Console.ReadLine();
}
}
}
One interesting aspect about params is that it will be invoked only if no other overloaded methods apply.
See the below program to have explanation of above sentence.
using System;
namespace Param_example1

Join the conversation! Your thoughts help the community grow.