Is there a C# alternative to Java’s vararg parameters?

Yes, C# has an equivalent of varargs parameters. They’re called parameter arrays, and introduced with the params modifier:

public void Foo(int x, params string[] values)

Then call it with:

Foo(10, "hello", "there");

Just as with Java, it’s only the last parameter which can vary like this. Note that (as with Java) a parameter of params object[] objects can easily cause confusion, as you need to remember whether a single argument of type object[] is meant to be wrapped again or not. Likewise for any nullable type, you need to remember whether a single argument of null will be treated as an array reference or a single array element. (I think the compiler only creates the array if it has to, but I tend to write code which avoids me having to remember that.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)