Use string.IsNullOrEmpty

Purpose

Converts an expression that checks whether a string is null or empty to the string.IsNullOrEmpty method call. This shortens the code and increases code performance.

Availability

Available when the cursor is within an expression checking whether a string is null or empty.

Usage

  1. Place the caret on an expression against a string checking whether it is null or empty.

    Note

    The blinking cursor shows the caret's position at which the Refactoring is available.

    public bool AddRecord(string name, object data) {
        if (name == null || name == string.Empty || data == null)
            return false;
    
        //... 
        return true;
    }
    
  2. Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
  3. Select Use string.IsNullOrEmpty from the menu.

After execution, the Refactoring replaces the logical expression with the string.IsNullOrEmpty method call.

public bool AddRecord(string name, object data) {
    if (string.IsNullOrEmpty(name) || data == null)
        return false;

    //... 
    return true;
}

See Also