MS アナライザーはstring.IsNullOrEmpty
、パフォーマンス上の理由から、null または空の文字列と比較する代わりに使用することをお勧めします
警告 470 CA1820 : Microsoft.Performance : ... の 'string.operator ==(string, string)' への呼び出しを 'String.IsNullOrEmpty' への呼び出しに置き換えてください。
何故ですか?別の関数を呼び出して何らかのオブジェクトへの参照を渡すという要件は、何らかの比較を実行する必要があるため、比較自体を実行するよりもコストがかかるのではないでしょうか?
サンプルコード
void Foo()
{ // throws a warning
string x = "hello world";
if (x == null || x == "")
{
Console.WriteLine("Empty");
}
}
void Foo()
{ // doesn't throw it
string x = "hello world";
if (string.IsNullOrEmpty(x))
{
Console.WriteLine("Empty");
}
}