MS Analyzer recommends to use string.IsNullOrEmpty
instead of comparising it either with null or empty string for performance reasons
Warning 470 CA1820 : Microsoft.Performance : Replace the call to 'string.operator ==(string, string)' in ... with a call to 'String.IsNullOrEmpty'.
Why is that? Shouldn't the requirement to call another function and pass it reference to some object, which then needs to execute some kind of comparison anyway, be more expensive than executing comparison itself?
Example code
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");
}
}