Alvaro Dias

String.IsNullOrEmpty() - boon or bane

So, as part of a functionality i wanted to implement, i needed to validate whether a string was null or empty.

Came up with 3 different ways: -

if(String.IsNullOrEmpty(myString)) // Do Work
if(myString != null && myString != "") // Do Work
if(myString != null && myString.Length != 0) // Do Work

Now, my task was to decide which one would be the best to use. The following blogs were of much help: -

Performance Analysis :

Issues with IsNullOrEmpty (at least read the 2nd post - the one on - it clarifies the issue with the said method):

Hopefully, these posts will help you too. Personally, I'd go for String.IsNullOrEmpty() just for the readability factor, unless performance is the bottleneck!!

Till my next post, Adeus