checking for empty string

  • Thread starter Thread starter Dan Bass
  • Start date Start date
D

Dan Bass

which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0
 
I usually go with:
MyString == null || MyString.Length == 0

Poking around a bit with Lutz Roeder's .Net Reflector, it seems that this
version is the equivalent of
MyString == null || MyString.m_stringLength == 0

while the other version if the equivalent of
MyString == null ||
CultureInfo.CurrentCulture.CompareInfo.Compare(MyString, new String (""),
CompareOptions.None) == 0

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0

Well, the latter is slightly more efficient, so you should use that if
this is a bottleneck in your application, but really it's a case of
whichever you find most readable, in most cases. I personally prefer
the first, but it's really a matter of taste.
 
It's a trade off between a slight advantage in clarity vs a slight advantage
in performance (actually MyString == "" has something more than a slight
performance advantage in theory, but in practice it isn't that often that it
amounts to so much of a difference that a user would actually notice).

I personally use MyString.Length == 0 out of habit, as I think it's pretty
self-explanatory.

As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it. It would also have the advantage of being utterly clear, except
that in my experience many developers seem fuzzy on exactly what Empty
really means. I mean, *I'm* clear on what it means yet when I first
encountered String.Empty I felt it necessary to check the docs to make sure
it meant what I thought it did. And most languages have an unambiguous way
to express an empty string as a constant similar to "", so I prefer "" as
clearer than String.Empty.

--Bob
 
I take his statement with a grain of salt. String.Empty is a string
instance that presumably already exists as he states (it's a static
instance), but "" not just "probably" but *wil*" come out of the string
intern pool, as do all string constants in programs. As even that blogger
admits, the difference would be insignificant in any case. And I've seen
more than one other confident statement that String.Empty and "" are exactly
identical. Personally I don't have the time to go through the Rotor source
to figure it out.

The truth is that these are the little rationalizations we create to
validate our favorite way of doing things. Value == "", Value ==
String.Empty, and String.Length == 0 are in fact all perfectly fine and in
almost all real-world scenarios even String.Length == 0 will not produce a
*significant* (user-noticeable) difference in performance.

Only we geeks can thoroughly debate a topic this unimportant to death ;-)

--Bob

LEBRUN Thomas said:
As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it

Not exactly ;)

The post of Brad Adams
(http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx) confirm that
using "" create an object while String.Empty create no onject.

So if you are really looking for ultimately in memory efficiency, use
String.empty

Bye.

-------------------
LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9

Bob Grommes said:
It's a trade off between a slight advantage in clarity vs a slight
advantage
in performance (actually MyString == "" has something more than a slight
performance advantage in theory, but in practice it isn't that often that
it
amounts to so much of a difference that a user would actually notice).

I personally use MyString.Length == 0 out of habit, as I think it's
pretty
self-explanatory.

As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it. It would also have the advantage of being utterly clear,
except
that in my experience many developers seem fuzzy on exactly what Empty
really means. I mean, *I'm* clear on what it means yet when I first
encountered String.Empty I felt it necessary to check the docs to make
sure
it meant what I thought it did. And most languages have an unambiguous
way
to express an empty string as a constant similar to "", so I prefer "" as
clearer than String.Empty.

--Bob

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0
 
LEBRUN Thomas said:
So if you are really looking for ultimately in memory efficiency, use
String.empty

FxCop tells me that testing String.Length against 0 is more efficient.
 
Micro-optimization, while interesting, is almost never a reason to write code in a particular way. Clarity and maintainability are hugely more important to me.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

LEBRUN Thomas said:
So if you are really looking for ultimately in memory efficiency, use
String.empty

FxCop tells me that testing String.Length against 0 is more efficient.
 
FxCop tells me that testing String.Length against 0 is more efficient.

Yes, it's right.

But the post was a discussion in what is the best between using "" or
String.Empty, not String.Length ;)

But, as Richard Blewett said, micro-optimization is not a reason for
changing the way you write code.

So if reducing execution time of your application is not your main purpose,
write your code withour changing your practices

Bye
 
Back
Top