By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

Why string.Empty is better then ""

P: n/a
hi
can u write me
Why string.Empty is better then ""

bye

Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"monu" <gt*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
hi
can u write me
Why string.Empty is better then ""


It is often argued that it more clearly exresses the intent of the coder. I
would usually agree with this.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #2

P: n/a
This has been brought up before. Most MS code uses String.Empty but from
what has been stated before it's real purpose is to simply provide languages
that don't directly support strings the ability to represent an empty string
anyway. For languages that do support strings (such as C#) then the "" is
fine. I would, in general, disagree that String.Empty is more clear than ""
because "" is far more concise and universally known as an empty string. The
only exception would be when it could be construed as a string with a single
space. This can sometimes be hard to discern depending on font size.
Performance wise however they are equivalent.

Michael Taylor - 9/19/05

"monu" wrote:
hi
can u write me
Why string.Empty is better then ""

bye

Nov 17 '05 #3

P: n/a
Tim Haughton wrote:
can u write me
Why string.Empty is better then ""


It is often argued that it more clearly exresses the intent of the coder. I
would usually agree with this.


Right, and it's theoretically more flexible/portable, because you don't
rely on the fact that "" is the empty string. Hard to come up with a
practical example here, but the String.Empty syntax simply introduces that
additional layer of abstraction. Personally, I like the fact that it's
there and I use it, although it doesn't really make any difference to the
workings of your code (yet?).
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #4

P: n/a
"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
Tim Haughton wrote:
can u write me
Why string.Empty is better then ""


It is often argued that it more clearly exresses the intent of the coder. Iwould usually agree with this.


Right, and it's theoretically more flexible/portable, because you don't
rely on the fact that "" is the empty string. Hard to come up with a
practical example here, but the String.Empty syntax simply introduces that
additional layer of abstraction. Personally, I like the fact that it's
there and I use it, although it doesn't really make any difference to the
workings of your code (yet?).


You're right on the portability front, just like I use Environment.NewLine
instead of "\r\n".

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

Nov 17 '05 #5

P: n/a
Oliver Sturm wrote:
Tim Haughton wrote:
can u write me
Why string.Empty is better then ""

<snip>

Also see:
http://blogs.msdn.com/brada/archive/.../22/49997.aspx

Looking at the IL generated, there's a difference, but both strings will
be pulled out of the string interning cache so there will be no
difference at runtime.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #6

P: n/a
Tim Haughton <ti*********@gmail.com> wrote:
You're right on the portability front, just like I use Environment.NewLine
instead of "\r\n".


I do, too - but only if I want the environment's 'new line' string.

If I actually want "\r\n" I use exactly that.
Nov 17 '05 #7

P: n/a
Tim Haughton <ti*********@gmail.com> wrote:
Right, and it's theoretically more flexible/portable, because you don't
rely on the fact that "" is the empty string. Hard to come up with a
practical example here, but the String.Empty syntax simply introduces that
additional layer of abstraction. Personally, I like the fact that it's
there and I use it, although it doesn't really make any difference to the
workings of your code (yet?).


You're right on the portability front, just like I use Environment.NewLine
instead of "\r\n".


No, there's no portability gain here. An empty string is an empty
string whatever platform you're on, whereas the new line varies by
system.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

P: n/a
Comments Inline
"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> schrieb im Newsbeitrag
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Oliver Sturm wrote:
Tim Haughton wrote:
can u write me
Why string.Empty is better then ""
<snip>

Also see:
http://blogs.msdn.com/brada/archive/.../22/49997.aspx

Looking at the IL generated, there's a difference, but both strings will


But there is no difference in the JIT-Compiled code!
be pulled out of the string interning cache so there will be no difference
at runtime.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.