Why string.Empty is better then ""

  • Thread starter Thread starter monu
  • Start date Start date
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
 
Tim said:
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
 
Oliver Sturm said:
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
 
Tim Haughton said:
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.
 
Tim Haughton said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top