views:

278

answers:

4

Possible Duplicate:
Checking for string contents? string Length Vs Empty String

In .NET, which is best,

if (mystring.Length == 0)

or

if (mystring == string.Empty)

It seems that these would have the same effect, but which would be best behind the scenes?

+4  A: 

The difference is so small I would consider it insignificant (the length property on a string is not calculated - it is a fixed value).

Andrew Hare
+41  A: 

I prefer

String.IsNullOrEmpty(myString)

Just in case it contains a null value.

kerchingo
String.IsNullOrEmpty(mystring)
Syed Tayyab Ali
+9  A: 

Use the one that makes the most sense to you, logically. The difference in performance is meaningless, so the "best" option is the one that you'll understand next year when you look back at this code.

My personal preference is to use:

if(string.IsNullOrEmpty(mystring))

I prefer this since it checks against null, too, which is a common issue.

Reed Copsey
+2  A: 

As long as your string isn't a null reference then it doesn't really matter, however in the event that it is, neither will work and you may want to consider string.IsNullOrEmpty(mystring);

Quintin Robinson