Tutorial :What is the difference between String.Empty and “” and null? [duplicate]


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

Is "" equivalent to String.Empty?

Which is preferred for initializing string values?


public sealed class String {      //...      public static readonly String Empty = "";      //...  }  

Use null when you want to represent that there is no value;

Use String.Empty when you want to represent that there is a value, but the value is a blank string.


String.Empty because it is a static variable, rather than "" which has to create a new string, and null means that you must then set the string equal to a new instance of a string.

(thanks for the correction)


Yes. And String.Empty, but please don't worry about it.

The 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 and String.Empty creates no object, so if you are really looking for ultimate 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…


Use whichever you find most readable.

I challenge anyone to find a realistic application where there's a significant performance difference... it just won't happen. However, different people find different approaches more readable.

Personally, I'm a "" person. I find it less cluttered, and I've never encountered a problem where I actually used " " (or something similar) accidentally. (That's one of the objections frequently raised.)

If you prefer string.Empty, I'm certainly not going to claim you're "wrong". I would suggest, however, that if you're working on a team you discuss it to find out what most people think is more readable, and stick to that. Consistency is generally a good thing.

EDIT: Just to allay some fears which might be induced by the claim that "" will create a new string... it may create a new string once (possibly per AppDomain, possibly per process; I'm not sure and it really doesn't matter). It won't create a new string every time you use it. For example, consider this code:

public void Foo()  {      string x = "";      for (int i=0; i < 10000000; i++)      {          string y = "";          // Do something here      }      string z = "";  }  

You are guaranteed that x, y and z will refer to the same string. At most, invoking Foo will mean a single empty string is created, and only the first time you call it, and only if you haven't used an empty string literal anywhere else in the program yet. The code above is not going to create 10 million new strings.


It is considered better practise to use string.Empty, however they are effectively equal. They are not the same as null, however.


FROM 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 String..::.Empty, use the IsNullOrEmpty method.


They are equal . But String.Empty is constant .

"" - is way of creating String .


The answer you are looking for is here.

What is the difference between String.Empty and "" (empty string)?

use String.Empty

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »