我在这里猜想,但这看起来是你要拨打的重载呼叫的不同之处;String.Format
有多个.
在第一个例子中,你达到String.Format(string,object)
是有道理的.
在第二个示例中,通过提供null
,您最有可能达到String.Format(string,params object[])
,根据文档,在以下情况下会产生ArgumentNullException
:
格式或参数为空.
如果你在 run .NET4,try 使用命名参数:
String.Format("Another exception occured: {0}", arg0: null);
为什么它会超负荷运行?可能是因为null
不是一个对象,params
的工作方式是,您可以在调用or中将either个值作为一个新对象传递给它,并向其传递一个值array.也就是说,以下是one in the same:
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();