Tuesday, May 10, 2011

what is the use of parms keyword in C#

here is the msdn link for Linkparams

E.g:

with params

void Main()
{
Method(1,2,3); // you can call it like this
}

static void Method(params int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}


without params

void Main()
{

Method(1,2,3); // throws error
}

static void Method (int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}

you have to do

void Main()
{
int[] i = {1,2,3};
Method(i);
}

static void Method (int[] array)
{
for(int i=0;i < array.Length;i++)
{
Console.WriteLine(array[i]);
}
}

No comments:

Post a Comment