Back to Devexpress

Use String.IsNullOrEmpty

coderushforroslyn-115615-refactoring-assistance-use-string-is-null-or-empty.md

latest1.6 KB
Original Source

Use String.IsNullOrEmpty

  • Aug 03, 2020
  • 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.

  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.

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

    //... 
    return true;
}
vb
Public Function AddRecord(ByVal name As String,
                          ByVal data As Object) As Boolean
    If String.IsNullOrEmpty(name) OrElse
        data Is Nothing Then
        Return False
    End If

    '... 
    Return True
End Function

See Also

Use String.Format

Use String.Compare

Use String Builder

Use Environment.NewLine