null
引数の 1 つにあるのでString.Format()
、 throws を呼び出します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
}
}
で区切られた 2 つの呼び出し以外の回避策はありますif()
か?