Difference between IsNullOrEmpty and IsNullOrWhiteSpace in C#

Asieh hojatoleslami picture Asieh hojatoleslami · Sep 10, 2013 · Viewed 100.5k times · Source

What are differences between these commands in C#

string text= "  ";
1-string.IsNullOrEmpty(text.Trim())

2-string.IsNullOrWhiteSpace(text)

Answer

fionbio picture fionbio · Sep 10, 2013

IsNullOrWhiteSpace is a convenience method that is similar to the following code, except that it offers superior performance:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;

White-space characters are defined by the Unicode standard. The IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character.