-1

Possible Duplicate:
What is the difference between String.Empty and “”

Hello

Simple Question;

Why

Textbox1.Text = String.Empty;

is better than

Textbox1.Text = "";

?

2

2 Answers 2

6

It's not, really.

Basically, decide which you find to be more readable. Personally I use "" instead of string.Empty, but others prefer the latter.

Back in .NET 1.x days apparently there was some tiny performance difference (almost certainly irrelevant in real apps) but I believe these days even that's gone.

Use whichever you and your find most readable.

Sign up to request clarification or add additional context in comments.

Comments

0

Because String.Empty is actually defined as ""

Best practices dictate not to use string literals but constants.

On a funnier note: String.Empty is/looks more Object Oriented-ISH.

5 Comments

Where it makes sense, use constants... but literals are fine in their place. Do you use int.Zero instead of 0, for example?
I never knew there was an int.Zero. I'm going to have to use that just because I can now, but not really. Unless the value of zero might change in the future.... or in another culture?..... Sounds like good fodder for a Dilbert comic.
@Jon I think numbers are ok to be used "as is" only if they are 0 or 1. Anything else should be made constant. So that is why I don't use int.Zero. However I don't feel the same thing about strings. But these are all guidelines not rules so they are not always 100% applicable but they usually are 99% of the time.
Why? Appealing to best practices is pointless unless you can explain why it's a best practice. For constants, the usual reason is that don't want to use the same value in multiple places, because it makes it hard to change the value later. That reason doesn't apply to string.Empty as you can't change that... if you have multiple places where you want to use the same logical value in a flexible way, by all means create a constant field for that... but using string.Empty provides no "best practice" benefit here.
I know this is a very old thread but one thing I've found, when working on an old undocumented code base, I have to treat any "" as either an actual empty string literal, or unfinished work because the previous dev never wrote it out and never left a note to finish it. The rather frequent time sink of having to double check this is small but a pain at times. string.Empty provides much much more clarity in those types of situations. In either scenario, prioritize what produces the clearest code and docs. If you are doing that already, then its tomato tomato

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.