140

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

How is String.Empty different from ""?

Community
  • 1
  • 1
Piyush
  • 5,145
  • 16
  • 49
  • 71

5 Answers5

113

It's not different.

http://msdn.microsoft.com/en-us/library/system.string.empty.aspx:

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.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • 1
    As the duplicate say, its not different in .NET 2 and above. – JoeBilly May 28 '10 at 12:27
  • 21
    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. – tekHedd Mar 27 '19 at 17:42
113

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Andy West
  • 12,302
  • 4
  • 34
  • 52
  • 3
    It is a lot faster to type two quotes than String.Empty. Two quotes is cross platform. It's a no brainer. – Brain2000 Dec 27 '18 at 19:02
  • 7
    I for one find `""` much easier to read and understand than `string.Empty`. – David R Tribble Aug 14 '19 at 15:56
  • 7
    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". – George Chakhidze Jan 27 '20 at 12:35
  • @GeorgeChakhidze you don't *need* string.Empty for that, as you could also introduce a single letter constant for the empty string while prototyping – symbiont Jul 25 '20 at 20:33
14

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

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • 5
    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. – Polyfun Dec 20 '18 at 15:39
11

For the most part String.Empty is identical to "" however usually I find its 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.

jamone
  • 17,253
  • 17
  • 63
  • 98
  • 65
    You miss the point: `String.IsNullOrEmpty(str)`has nothing to do with `string.Empty`, except that the word empty appear in both places. – Heinz Kessler Jun 17 '17 at 08:57
  • 2
    `str == "" || str == null` looks quite ineffective to me, given that the left side is evaluated first. Just another reason to use the `IsNullOrEmpty` method :-) – TToni Jul 02 '20 at 13:16
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
Marks
  • 3,613
  • 5
  • 31
  • 46
  • 11
    A new object is not created every time. http://en.wikipedia.org/wiki/String_interning – Josh Stodola May 25 '10 at 14:09
  • 1
    The compiler will "link" them all to an empty string. – cyberzed May 25 '10 at 14:10
  • 2
    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*! ;-) – mcrumley Jun 02 '10 at 14:05
  • 11
    Did you mean "The main reason for NOT using String.Empty is readability"? – BlackTigerX May 13 '11 at 21:13