我在所谓的 throwsnull
中有一个论点。为什么即使参数不在结果字符串中也会进行检查?String.Format()
NullReferenceException
class Foo
{
public Exception Ex { get; set; }
}
class Program
{
public static void Main(string[] args)
{
var f1 = new Foo() { Ex = new Exception("Whatever") };
var f2 = new Foo();
var error1 = String.Format((f1.Ex == null) ? "Eror" : "Error: {0}", f1.Ex.Message); // works
var error2 = String.Format((f2.Ex == null) ? "Eror" : "Error: {0}", f2.Ex.Message); // NullReferenceException
}
}
除了用 分隔的两个调用外,还有其他解决方法if()
吗?