The difference is that the first part of the code calls string.Format(string, object[]) ... while the second part of the code calls string.Format(string, object) .
null is a valid argument for the second method (it should have been considered the value for the first placeholder), but not the first (where null usually an array of placeholders). In particular, compare the documentation when a NullArgumentException is NullArgumentException :
string.Format(string, object) :
null format
But:
string.Format(string, object[]) :
format or args null
Think of string.Format(string, object) as an implementation of something like:
public static string Format(string format, Object arg0) { return string.Format(format, new object[] { arg0 } ); }
So, after a bit of replacement, your code is closer to:
// Broken code object[] args = null; // No array at all var test = string.Format("{0}", args); // Working code object[] args = new object[] { null }; // Array with 1 value var test = string.Format("{0}", args);
Jon Skeet Jul 01 2018-11-14T00: 00Z
source share