Assume I define a method like this:
public static void ListInts(int[] inVals)
{
for (int i = 0; i < inVals.Length; i++)
{
Console.WriteLine("{0}", inVals[i]);
}
}
If I try call it like this, obviously i'm getting an error and code will not compile:
ListInts();
Method 'ListInts' has one parameter(s) but is invoked with 0 arguments
But when i change method definition like this:
public static void ListInts(params int[] inVals)
{
for (int i = 0; i < inVals.Length; i++)
{
Console.WriteLine("{0}", inVals[i]);
}
}
There is no error.I'm curious why this is happening ? And why second code is compiling without any errors? What happens behind the scenes when we use params
keyword ?
You could have easily search for the reason in the documentation.
From MSDN: