views:

384

answers:

8
+2  A: 

It doesn't matter, it's not any difference as described in MSDN article.

String.Empty Field

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

Ondrej Slinták
+4  A: 

Its good to use String.Empty rather than ""

myString = “” creates a new instance of type String.

myString = String.Empty does not. 

EDIT

Misconception that using an empty quoted string always instantiates a new object.The question in your mind will be arising that why I am saying like that . Note that .NET interns its strings, so future instances will pull the same immutable string from the intern pool, and any performance hit will be negligible. In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool. The single copy of each string is called its 'intern' and is typically looked up by a method of the string class.What that means is ok if “” creates object while String.Empty does not. But this object will be created once and will be referenced from the string pool later if you have another "" in the code. String interning keeps a hashtable of strings while running an application. If a string with the same contents is created, no new heap allocation happens but instead a reference to the existing (identical) string is returned.Keep in mind each string literal does not necessarily result in a new string instance.

Difference between String.Empty and "" (doublequotes)

String.Empty vs “” vs String. IsNullOrEmpty

Pranay Rana
myString = “” wont create a new instance.http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or
anishmarokey
-1. This is not right. only legible reason to use string.Empty over "" would be readability and nothing else. M surprised to see so many upvotes. The link actually talk about the diff. between string.Empty and string.IsNullOrEmpty().
this. __curious_geek
@this.__curious_geek Because nobody really read linked pages. It's enough to assert in your answer, that linked page confirms what you wrote. ;-)
Tomek Szpakowicz
-1. This is indeed wrong.
Steven
http://msdn.microsoft.com/en-us/library/system.string.empty.aspx it says `The value of this field is the zero-length string, "".` I don't know enough C# to say if the field object is copied or referenced, ...
ShinTakezou
+5  A: 

Use whatever your project or team style guidelines say. And if you have none, just chat about it for a moment, choose one and use it consistently.

Technically both are identical and lead to the same code. String.Empty represents instance of interned empty string (""). And since all in-code string constants are automatically interned, "" represents exactly the same instance of empty string (Update: Within assembly, see update below).


And by the way, I'm using "". It's short, clear and readable.

String.Empty is useful in generic code using reflection etc. When you don't care what class you have, just want to get empty value for e.g. initialization with default value.


Update: It appears that it's not that simple. There might be many instances of interned string within single app domain. And the behavior depends on framework version. For more information see Eric Lippert's String interning and String.Empty and remarks in Reference for String.Intern method.

But the second link suggest that since 3.5 SP1 there is again single interned "", as it should be in the first place. So, why bother with it when you program? Projects take some time to complete. Factor in improvements in compilers, frameworks and tools... And don't optimize prematurely. It's more important to be readable, consistent and correct.

Tomek Szpakowicz
+6  A: 

String.Empty is what is normally recommended.

Why? Because it conveys meaning, you are very explicitly saying that you want the string to be empty.

Additionally, "" creates a string object (this object will be reused across the applications, because strings in .NET are interned), String.Empty does not create a new object. See here.

Oded
So, does "" create a new object, or reuse interned object? Because you got me lost here...
Tomek Szpakowicz
See also string.IsNullOrEmpty(s), which is preferred over if (s != "")
Jason Williams
@tomekszpakowicz - Perhaps this will help: http://msdn.microsoft.com/en-us/library/system.string.intern.aspx
Oded
@Oded: No comment. For a moment I had hope for some thinking instead of repeating a fairy tale about magical powers of String.Empty. Perhaps this will help: http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or/263257#263257
Tomek Szpakowicz
@tomekszpakowicz - what are you talking about? Where have I said anything about "magical powers of String.Empty"??? I have said that there is hardly a difference.
Oded
tomekszpakowicz - String.Empty is probably a static member which is initialised into memory by default within the .NET framework. "" will not be loaded in unless it is explicitly used in your code somewhere. Therefore, there is a very tiny performance benefit to using String.Empty (not to mention the readability benefit).
C.McAtackney
@Oded: You said that there is a performance gain in using String.Empty.
Tomek Szpakowicz
@tomekszpakowicz - I said there was a _slight_ one, that it is _not significant_. This is based on different micro benchmarks posted on different websites.
Oded
@Oded It would be great if you could add links to those benchmarks to your answer.
Tomek Szpakowicz
@tomekszpakowicz - I could swear I have seen these benchmarks, but can't for the life of me find them now. Answer updated, removed line about performance.
Oded
+2  A: 

A good article explaining the differences between "" and string.Empty can be found here.

From the article:

"Here we looked in depth at string.Empty, first seeing how to use it in your program and then reviewing why it won't work in switches. Additionally, we found that it provides less information to the C# compiler about your code than the constant itself. Finally, we saw that the C# compiler can completely eliminate certain instructions that use "", but not those that use string.Empty"

Ando
+1  A: 

From what I've read there's no difference in behavior, and very, very little difference in efficiency between the two (with the very slight edge going to string.Empty). So it doesn't really matter which one you choose, there's no practical difference.

allonym
A: 

Better to use string.Empty than "".

string.Empty is more readable code than "".

better to use string variableName = string.Empty; not string variableName = "";

Note: there is not object creation for "" .Net 3.0 and above.Both "" and String.Empty are same.

But in .Net 2.0 "" will create a new instance and not for String.Empty

http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or

http://www.ecma-international.org/publications/standards/Ecma-334.htm

anishmarokey
A: 

" " and "" look similar. if you cannot catch a space inside " " by mistake

then " " can pass String.IsNullOrEmpty(). and make unexpected result.

sunglim
Except when being (almost) blind or using a proportional font, I don't think it's easy to mix up "" and " "
Christian