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 : http://www.dotnetperls.com/isnullorempty

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

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=113102&wa=wsignin1.0

http://www.omegacoder.com/?p=105

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