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?
I prefer
String.IsNullOrEmpty(myString)
Just in case it contains a null value.
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.
The difference is so small I would consider it insignificant (the length property on a string is not calculated - it is a fixed value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With