Skip to main content

Use String.IsNullOrEmpty

  • 2 minutes to read

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. Press 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