What is the best way for checking empty strings (I’m not asking about initializing!) in C# when considering code performance?(see code below)
string a;
// some code here.......
if(a == string.Empty)
or
if(string.IsNullOrEmpty(a))
or
if(a == "")
any help would be appreciated. 🙂
Do not compare strings to
String.Emptyor""to check for empty strings.Instead, compare by using
String.Length == 0The difference between
string.Emptyand""is very small.String.Emptywill not create any object while""will create a new object in the memory for the checking. Hence string.empty is better in memory management.But the comparison with
string.Length == 0will be even faster and the better way to check for the empty string.