Skip to main content

Use IsNullOrEmpty

Converts an expression that checks whether a string is null or empty to a String.IsNUllOrEmpty call.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is anywhere within an expression checking whether a string is null or empty.

    Note

    If the initial expression checks a string only for null or only for empty, the Use IsNullOrEmpty code provider is available instead of the refactoring.

#Example

private string ProcessText(string text)
{
  if (text==null || text==string.Empty)
    return string.Empty;
  return text.Replace('_',' ');
}
Private Function ProcessText(ByVal text As String) As String
  If text = Nothing Or text = String.Empty Then
    Return String.Empty
  End If
  Return text.Replace("_", " ")
End Function

Result:

private string ProcessText(string text)
{
  if(String.IsNullOrEmpty(text))
    return string.Empty;
  return text.Replace('_', ' ');
}
Private Function ProcessText(ByVal text As String) As String
  If String.IsNullOrEmpty(text) Then
    Return String.Empty
  End If
  Return text.Replace("_", " ")
End Function

#Screenshot

rsUseIsNullOrEmpty