Just decompile the code to find out what is going on.
string.Format("{0}", null)
causes the most specific applicable overload, which is string.Format(string, object[]) .
The overloads of string.Format are:
Format(String, Object) Format(String, Object[]) Format(IFormatProvider, String, Object[]) Format(String, Object, Object) Format(String, Object, Object, Object)
Hopefully this is obvious why the last three options are invalid.
To determine which of the first two to use, the compiler compares the conversion from null to Object to the conversion from null to Object[] . Converting to Object[] is considered "better" because there is a conversion from Object[] to Object , but not vice versa. This is the same logic with which we had:
Foo(String) Foo(Object)
and called Foo(null) , he would choose Foo(String) .
So your source code is equivalent:
object[] values = null; string.Format("{0}", values);
At this point, I hope you expect an ArgumentNullException - as per the documentation.
Jon Skeet Dec 14 '12 at 11:16 2012-12-14 11:16
source share