P
PIEBALD
Given a method like
void F ( params object[] args ) { ... }
I had always _assumed_ that args would never be null, but now it's been
brought to my attention that if the caller uses
F ( null )
then args _will_ be null because the compiler can implicitly convert the
null to an object[].
But I doubt that this is the desired behaviour in most cases, certainly _I_
expect (and prefer) args to be an array containing one null, just as
F ( null , null )
results in an array containing two nulls.
Some experimentation has shown that
F ( new object[] { null } )
and
F ( (object) null )
result in the desired array, but the caller shouldn't have to go through
such hoops.
It seems to me that rather than have the compiler see if the value can be
implicitly converted to the array type, it shoud determine if the value _is_
the array type.
I realize that it's too late to change the language, and if someone _does_
want the parameter to be null, then the current behaviour seems the only way.
But, man, now I have to go through my code looking for methods that take
params and protect against null.
void F ( params object[] args ) { ... }
I had always _assumed_ that args would never be null, but now it's been
brought to my attention that if the caller uses
F ( null )
then args _will_ be null because the compiler can implicitly convert the
null to an object[].
But I doubt that this is the desired behaviour in most cases, certainly _I_
expect (and prefer) args to be an array containing one null, just as
F ( null , null )
results in an array containing two nulls.
Some experimentation has shown that
F ( new object[] { null } )
and
F ( (object) null )
result in the desired array, but the caller shouldn't have to go through
such hoops.
It seems to me that rather than have the compiler see if the value can be
implicitly converted to the array type, it shoud determine if the value _is_
the array type.
I realize that it's too late to change the language, and if someone _does_
want the parameter to be null, then the current behaviour seems the only way.
But, man, now I have to go through my code looking for methods that take
params and protect against null.