Jūs esate

Passing parameters in C#

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


O kaip, jeigu nori skirtingų tipų perduoti? :)

Tada kaip objektus:
void Method(params object[] parameters)
foreach (object obj in parameters)
Console.WriteLine(obj + ": " + obj.GetType());
// Kodas....
Method(4, "1", true);
// Rezultatas:
4: System.Int32
1: System.String
True: System.Boolean

Kaip static-typing, tai nice matyt tokius bajerius ;)

Ant java rodos taip neina...