我在这里猜测,但这看起来是你所调用的重载调用的区别;String.Format http://msdn.microsoft.com/en-us/library/xh1dzhdx.aspx有多个。
在第一个例子中,你打的是有意义的String.Format(string,object) http://msdn.microsoft.com/en-us/library/fht0f5be.aspx.
在第二个示例中,通过提供null
你很可能会击中String.Format(string,params object[]) http://msdn.microsoft.com/en-us/library/b1csw23d.aspx根据文档,这会引发ArgumentNullException
when:
格式或参数为空。
如果您运行的是 .NET4,请尝试使用命名参数:
String.Format("Another exception occured: {0}", arg0: null);
为什么它会击中params object[]
超载?大概是因为null
不是一个对象,并且方式params
作品是你可以通过either每个值作为调用中的新对象or向其传递一个值数组。也就是说,以下是一在同一个 http://ideone.com/M9Hl7C:
String.Format("Hello, {0}! Today is {1}.", "World", "Sunny");
String.Format("Hello, {0}! Today is {1}.", new Object[]{ "World", "Sunny" })
因此,它将您的语句调用转换为以下内容:
String format = "Another exception occured: {0}";
Object[] args = null;
String.Format(format, args); // throw new ArgumentNullException();