Published on

C# - compare string is null or empty

There have 3 string variable

string strA = null; // null
string strB = ""; // empty
string strC = " "; // whitespace

1.using == , just can found out is null or is empty single

(strA == null) //true
(strB == null) //false
(strC == null) //false
(strA == "") //false
(strB == "") //true
(strC == "") //false

2.using trim() extension can found out is empty or is whitespace, but maybe have throw exception when the string is null

(strA.Trim() == "") //throw exception
(strB.Trim() == "") //true
(strC.Trim() == "") //true

3.using string.IsNullOrEmpty , can found out is null and empty both, but can't found out whitespace

string.IsNullOrEmpty(strA) //true
string.IsNullOrEmpty(strB) //true
string.IsNullOrEmpty(strC) //false

4.using string.IsNullOrWhiteSpace , can found out is null and empty and whitespace, but the method maybe have performance issues

string.IsNullOrWhiteSpace(strA) //true
string.IsNullOrWhiteSpace(strB) //true
string.IsNullOrWhiteSpace(strC) //true

so, best way is using string.IsNullOrEmpty()

if not need have whitespace condition or using string.IsNullOrWhiteSpace() have whitespace condition