Back to Devexpress

CRR0051 - String.IsNullOrEmpty can be used

coderushforroslyn-401177-static-code-analysis-analyzers-library-crr0051-string-null-or-empty-can-be-used.md

latest1.6 KB
Original Source

CRR0051 - String.IsNullOrEmpty can be used

  • Feb 28, 2025

This analyzer identifies expressions that test a string for null ( Nothing in Visual Basic) or an empty value, which can be replaced with a string.IsNullOrEmpty method call.

csharp
public bool AddRecord(string name, object data) {
    if (name == null || name == string.Empty || data == null)
        return false;
    //...
    return true;
}
vb
Public Function AddRecord(ByVal name As String, ByVal data As Object) As Boolean
    If name Is Nothing OrElse name = String.Empty OrElse data Is Nothing Then
        Return False
    End If

    '... 
    Return True
End Function

To fix this issue, use the string.IsNullOrEmpty method call instead of logical expressions:

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

Call the Use string.IsNullOrEmpty refactoring to replace the logical expression with the string.IsNullOrEmpty method call.

See Also

Suppress Analyzers