Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Hi Everyone....
Can anyone tell me what is difference between sting a=string.empty and string b="" ?
Posted
Comments
Sergey Alexandrovich Kryukov 15-Oct-13 15:10pm    
Now, it's null vs. "". vs. String.Empty. (And string.empty would not even compile. Is it another option? :-)
"" is not null.
What's wrong with reading some documentation before you ask a question?
—SA
Bernhard Hiller 16-Oct-13 2:39am    
Also note that beyond the programming concepts of null and empty strings, they may be the same from a requirements point of view: the user did not provide data. Just llok at the "paper" version of the work flow: if a user does not fill in a field, is it empty or null?
I guess that that's also the reason why Oracle database does not allow an empty string when the column has a NOT NULL constraint.

Your title and the text contain two different questions.

String.Empty and "" are the same from programming point of view. See: http://msdn.microsoft.com/en-us/library/system.string.empty.aspx[^]

A string is an object, something that has reserved space in memory. The variable itself contains a reference to that address space (sort of). If a reference (the variable's very own value) is null, that means that the content it is referring to (the string itself) does not exist. It is uninitialized. While both string.Empty, String.Empty and "" mean that the string itself is initialized with an empty (zero-length) value.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Oct-13 15:09pm    
Exactly, a 5.
—SA
Sergey Alexandrovich Kryukov 15-Oct-13 15:22pm    
On second though, "" vs. String.Empty is not as trivial. This is related to the intern pool functionality. Please see my answer.
—SA
ridoy 15-Oct-13 16:30pm    
5ed!
The difference is the same as writing the question with empty text (I hope this is prevented by CodeProject software though) and not posting any question at all. Note that the second option would be much better: you could easily learn this matter by reading original documentation.

Note that the question is inconsistent (please see my comment to the question and other answers).

The problem with null is trivial, but "" vs. String.Empty is not so simple. First would work like immediate constant and the second one is the read-only field. They make the same result, but due to a very non-trivial feature of strings, specifically: string interning. Please see:
http://en.wikipedia.org/wiki/String_interning[^],
http://broadcast.oreilly.com/2010/08/understanding-c-stringintern-m.html[^].

In not intern pool, we could possibly end up having multiple "" strings in different parts of code. With intern pool, it doesn't happen.

Practically, it's important to use String.Empty, not an immediate constant. Immediate constants make code dirty, are bad for maintenance. Exclusions could be made for immediate constants like 0, 1 and null, but never "". Imaging you want to remove all hard-coded strings from the code, using text search. Would be a good idea. And then "" will get in your search result, so it's better to get rid of them in the very beginning.

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900