-1

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

Hello

Simple Question;

Why

Textbox1.Text = String.Empty;

is better than

Textbox1.Text = "";

?

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

2 Answers2

7

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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.

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65
  • 5
    Where it makes sense, use constants... but literals are fine in their place. Do you use `int.Zero` instead of 0, for example? – Jon Skeet Dec 13 '10 at 13:50
  • 1
    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. – Brad Dec 13 '10 at 13:53
  • @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. – Liviu Mandras Dec 13 '10 at 13:56
  • 3
    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. – Jon Skeet Dec 13 '10 at 14:04
  • 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` – Narish Aug 02 '22 at 13:34