469,632 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

checking for empty string

which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0
Nov 16 '05 #1
11 4263
Checking the length is better than checking if the string is empty or not :)

Bye.

-------------------
LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9
"Dan Bass" wrote:
which one do you use and why?

MyString == null || MyString == ""

vs

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

Nov 16 '05 #2
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(MyS tring, 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

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... which one do you use and why?

MyString == null || MyString == ""

vs

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

Nov 16 '05 #3
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
I use

MyString == null || MyString == string.Empty

as I think its the clearest with hard coded values

Regards

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

which one do you use and why?

MyString == null || MyString == ""

vs

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

Nov 16 '05 #5
should say "without hard coded values"

Regards

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

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#I**************@TK2MSFTNGP12.phx.gbl>

I use

MyString == null || MyString == string.Empty

as I think its the clearest with hard coded values
Nov 16 '05 #6
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
news:%2****************@TK2MSFTNGP10.phx.gbl...
which one do you use and why?

MyString == null || MyString == ""

vs

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

Nov 16 '05 #7
> 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/.../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" wrote:
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
news:%2****************@TK2MSFTNGP10.phx.gbl...
which one do you use and why?

MyString == null || MyString == ""

vs

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


Nov 16 '05 #8
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" <lebrun_thomas_at_hotmail.com> wrote in message
news:02**********************************@microsof t.com...
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/.../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" wrote:
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
news:%2****************@TK2MSFTNGP10.phx.gbl...
> which one do you use and why?
>
> MyString == null || MyString == ""
>
> vs
>
> MyString == null || MyString.Length == 0
>


Nov 16 '05 #9
LEBRUN Thomas <lebrun_thomas_at_hotmail.com> wrote:
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.
Nov 16 '05 #10
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 <lebrun_thomas_at_hotmail.com> wrote:
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.

Nov 16 '05 #11
> 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

-------------------
LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9
"C# Learner" wrote:
LEBRUN Thomas <lebrun_thomas_at_hotmail.com> wrote:
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.

Nov 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Antoni | last post: by
14 posts views Thread by Christopher Benson-Manica | last post: by
6 posts views Thread by dwight | last post: by
42 posts views Thread by =?Utf-8?B?UGxheWE=?= | last post: by
3 posts views Thread by Nader | last post: by
1 post views Thread by =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.