[英]String.Format with null format
谁能解释为什么会发生以下情况:
String.Format(null, "foo") // Returns foo
String.Format((string)null, "foo") // Throws ArgumentNullException:
// Value cannot be null.
// Parameter name: format
谢谢。
它称之为不同的超载。
string.Format(null, "");
//calls
public static string Format(IFormatProvider provider, string format, params object[] args);
MSDN方法链接描述如上。
string.Format((string)null, "");
//Calls (and this one throws ArgumentException)
public static string Format(string format, object arg0);
MSDN方法链接描述如上。
因为调用哪个重载函数在编译时根据参数的静态类型确定:
String.Format(null, "foo")
使用空的IFormatProvider和格式化字符串“foo”调用String.Format(IFormatProvider, string, params Object[])
,这非常好。
另一方面,
String.Format((string)null, "foo")
调用String.Format(string, object)
,将null作为格式化字符串,抛出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.