Skip to content

Gendarme.Rules.Performance.CompareWithEmptyStringEfficientlyRule(2.10)

Sebastien Pouliot edited this page Jan 22, 2011 · 2 revisions

CompareWithEmptyStringEfficientlyRule

Assembly: Gendarme.Rules.Performance
Version: 2.10

Description

This rule will fire if a string is compared to "" or String.Empty. Instead use a String.Length test which should be a bit faster. Another possibility (with .NET 2.0) is to use the static String.IsNullOrEmpty method. String.IsNullOrEmpty.

Examples

Bad example:

public void SimpleMethod (string myString)
{
    if (myString.Equals (String.Empty)) {
    }
}

Good example:

public void SimpleMethod (string myString)
{
    if (myString.Length == 0) {
    }
}
Clone this wiki locally