Archyvas

Įrašo tag'ai: ‘variable’

Passing parameters in C#

Balandis 22nd, 2009 Ernestas Kardzys 3 komentarai

In C we can use a function printf(), which can take different numbers of parameters. E.g.: printf(“%d %s”, a, b). How this can be done in C#?

It can be done by keyword params:

int Method(params int[] var)
{
int sum = 0;
foreach (int i in var)
sum += i;
return sum;
}

// Code…

Method(5, 2, 3);

Method(64, 1);

Method(9);

More information: http://msdn.microsoft.com/en-us/library/w5zay9db(VS.71).aspx