I have a method that uses the params
keyword, like so:
private void ParamsMethod(params string[] args) { // Etc... }
Then, I call the method using various combinations of arguments:
// Within the method, args is... ParamsMethod(); // - a string array with no elements ParamsMethod(null); // - null (Why is this?) ParamsMethod((string)null); // - a string array with one element: null ParamsMethod(null, null); // - a string array with two elements: null and null ParamsMethod("s1"); // - a string array with one element: "s1" ParamsMethod("s1", "s2"); // - a string array with two elements: "s1" and "s2"
I understand all of the cases, except for the second one. Can someone explain why ParamsMethod(null)
causes args
to be null
, instead of an array with one null element?
You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.
So yes, it is entirely possible for the array associated with params to be null.
The Null object is a special instance of a class which represents missing value. If some method expects an object as a parameter, you can always pass the Null object representation without worry it will cause an unexpected exception at the runtime.
The parameter array is automatically optional. Its default value is an empty one-dimensional array of the parameter array's element type. All parameters preceding the parameter array must be required. The parameter array must be the only optional parameter.
A params
parameter is only meant to provide a convenient way of specifying values - you can still pass an array reference directly.
Now, null
is convertible to either string[]
or string
, so both interpretations are valid - it's up to the spec which is preferred. The spec states in section 10.6.1.4 that:
The argument given for a parameter array can be a single expression that is implicitly convertible to the parameter array type. In this case, the parameter array acts precisely like a value parameter.
Alternatively, [...]
In other words, the compiler checks to see whether the argument is valid as the "normal" parameter type first, and only builds an array if it absolutely has to.
See the C# specification, section 10.6.1.4 Parameter arrays:
A parameter array permits arguments to be specified in one of two ways in a method invocation:
- The argument given for a parameter array can be a single expression that is implicitly convertible (§6.1) to the parameter array type. In this case, the parameter array acts precisely like a value parameter.
- Alternatively, the invocation can specify zero or more arguments for the parameter array, where each argument is an expression that is implicitly convertible (§6.1) to the element type of the parameter array. In this case, the invocation creates an instance of the parameter array type with a length corresponding to the number of arguments, initializes the elements of the array instance with the given argument values, and uses the newly created array instance as the actual argument.
Since null
is implicitly convertible to string[]
, it will be used as the array.
Furthermore:
When performing overload resolution, a method with a parameter array may be applicable either in its normal form or in its expanded form (§7.5.3.1). The expanded form of a method is available only if the normal form of the method is not applicable and only if a method with the same signature as the expanded form is not already declared in the same type.
which clarifies that the compiler indeed first tries to use the method with an array argument (normal form) before it tries using the argument as an array element (expanded form).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With