6

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

Which of the following should I use for assigning an empty string and why?

string variableName = ""; 

or

string variableName = string.Empty; 
4

8 Answers 8

6

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.

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

10 Comments

So, does "" create a new object, or reuse interned object? Because you got me lost here...
See also string.IsNullOrEmpty(s), which is preferred over if (s != "")
@tomekszpakowicz - Perhaps this will help: msdn.microsoft.com/en-us/library/system.string.intern.aspx
@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: stackoverflow.com/questions/263191/…
@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.
|
6

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.

Comments

5

It's good to use String.Empty rather than "".

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

EDIT

The misconception is that using an empty quoted string always instantiates a new object. Why am I am saying it 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 take 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 an 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

4 Comments

myString = “” wont create a new instance. stackoverflow.com/questions/263191/…
-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 Because nobody really read linked pages. It's enough to assert in your answer, that linked page confirms what you wrote. ;-)
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, ...
2

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, "".

Comments

2

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"

Comments

1

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.

Comments

0

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

In C#, should I use string.Empty or String.Empty or "" to intitialize a string?

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

Comments

0

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

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.