156

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

How is String.Empty different from ""?

3

5 Answers 5

122

According to Brad Abrams:

As David implies, there difference between String.Empty and “” are pretty small, but there is a difference. “” actually creates an object, it will likely be pulled out of the string intern pool, but still… while String.Empty creates no object… so if you are really looking for ultimately in memory efficiency, I suggest String.Empty. However, you should keep in mind the difference is so trival you will like never see it in your code...

As for System.String.Empty or string.Empty or String.Empty… my care level is low ;-)

Update (6/3/2015):

It has been mentioned in the comments that the above quote from 2003 is no longer true (I assume this is referring to the statement that "" actually creates an object). So I just created a couple of simple console programs in C# 5 (VS 2013):

class Program { static void Main() { // Outputs "True" Debug.WriteLine(string.IsInterned(string.Empty) != null); } } class Program { static void Main() { // Outputs "True" Debug.WriteLine(string.IsInterned("") != null); } } 

This demonstrates that both "" and String.Empty are both interned when your code starts running, meaning that for all practical purposes they are the same..

The most important part of Brad's comment is this:

you should keep in mind the difference is so trival you will like never see it in your code...

That's the bottom line. Choosing between "" and String.Empty is not a performance-based decision. Don't waste a lot of time thinking about it. Choose based on whatever you find more readable, or whatever convention is already being used in your project.

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

4 Comments

It is a lot faster to type two quotes than String.Empty. Two quotes is cross platform. It's a no brainer.
I for one find "" much easier to read and understand than string.Empty.
I've had numerous cases while quickly prototyping code, I littered "" all over the place, which later introduced many bugs, because intent was not clear: "Did I mean to say: pass empty string here, intentionally" or "Did I forgot to pass a non-empty string here".
@GeorgeChakhidze you don't need string.Empty for that, as you could also introduce a single letter constant for the empty string while prototyping
118

It's not different.

https://learn.microsoft.com/dotnet/api/system.string.empty:

The value of this field is the zero-length string, "".

In application code, this field is most commonly used in assignments to initialize a string variable to an empty string. To test whether the value of a string is either null or String.Empty, use the IsNullOrEmpty method.

2 Comments

As the duplicate say, its not different in .NET 2 and above.
In the .NET 1 era, it was different. So, you'll see String.Empty in old code that is still in use, and reflexively entered by old-schoolers who suffered through the .NET 1 era. Be kind; it left a mark.
16

There is no difference. Some prefer to use String.Empty for code readability purposes. Use the one you are comfortable with using.

1 Comment

In my opinion, String.Empty is not easier to read. I don't like it. I always use "". We use double quotes for non-empty string literals, so to be consistent we should double quotes for string literals too.
10

For the most part, String.Empty is identical to "". However, I usually find it's easier to use String.IsNullOrEmpty(str) instead of having to compare str == "" || str == null. Also, if you are on .NET 4.0, String.IsNullOrWhiteSpace(str) covers even more cases and is by far the best.

2 Comments

You miss the point: String.IsNullOrEmpty(str)has nothing to do with string.Empty, except that the word empty appear in both places.
str == "" || str == null looks quite ineffective to me, given that the left side is evaluated first. Just another reason to use the IsNullOrEmpty method :-)
3

string.Empty is always the same object

"" is always a new object, which may have to be removed by the garbage collector.

Therefore you should always use string.empty instead of "".

string a=""; string b=string.Empty; 

is translate to

IL_0000: ldstr "" IL_0005: ldsfld System.String.Empty 

5 Comments

A new object is not created every time. en.wikipedia.org/wiki/String_interning
The compiler will "link" them all to an empty string.
I read somewhere (sorry, I don't remember where) that the empty string is duplicated once in each assembly. So each assembly that uses "" instead of System.String.Empty is wasting valuable memory! ;-)
Did you mean "The main reason for NOT using String.Empty is readability"?
Rehashing an old thread just for fun: This answer was once true, but not anymore. Rest In Peace

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.