C# Language "params" is not expanded, unless necessary.


The following program:

class Program
    static void Method(params Object[] objects)
    static void Method(Object a, Object b)
    static void Main(string[] args)
        object[] objectArray = new object[5];

        Method(objectArray, objectArray);
        Method(objectArray, objectArray, objectArray);

will print:


The call expression Method(objectArray) could be interpreted in two ways: a single Object argument that happens to be an array (so the program would output 1 because that would be the number of arguments, or as an array of arguments, given in the normal form, as though the method Method did not have the keyword params. In these situations, the normal, non-expanded form always takes precedence. So, the program outputs 5.

In the second expression, Method(objectArray, objectArray), both the expanded form of the first method and the traditional second method are applicable. In this case also, non-expanded forms take precedence, so the program prints two.

In the third expression, Method(objectArray, objectArray, objectArray), the only option is to use the expanded form of the first method, and so the program prints 3.