Click here to Skip to main content
14,488,259 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone!What is the difference between String.Empty and ""?
for example ,

string s1=String.Empty;
string s2="";
Updated 1-Apr-19 19:21pm
Rate this:
Please Sign up or sign in to vote.

Solution 4

Placing magic values in code is typically frowned upon, for a variety of reasons (some of which have already been explained). Though string.Empty seems like a slightly different beast (e.g., because you do not actually have the ability to modify the constant, which is one of the reasons for using constants).

Here is one practical difference: you can right click on "string.Empty" and do a "find all references" to find where empty strings are used in your code. You cannot do that with an empty string assigned using double quotes.

Also, if you are trying to remove magic values from your code, you might as well make use of this existing constant so that empty strings don't trip you up when searching through your code for quotes that should not be there (e.g., if you have a single class that holds all your strings, such as a resource file that gets translated into a class, you could search all other files to ensure they don't contain quotes).
Sergey Alexandrovich Kryukov 20-Dec-10 22:54pm
This is wise.

I usually do not tolerate any immediate string constants in code and confine any constant definitions to very few special files and static classes.

If I had to make exclusion for "", cleaning code would be much more difficult.
I never do this exclusion (event though string.Empty is not allowed when attribute is allowed), so I can, for example, simply perform the search for '"' (single quote) and exterminate offending code.
Abhinav S 21-Dec-10 0:07am
Good answer.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Adding to what ashokbusybee corectly said, there are instances when you must use "", for example:
public string MyStringProperty
    // Accessors

Try to put string.Empty there!
hemantwithu 24-Sep-10 7:37am
Sergey Alexandrovich Kryukov 20-Dec-10 18:55pm
Toli, you should have added: this is because string.Empty is readonly static field, where only constant is accepted.
Sergey Alexandrovich Kryukov 20-Dec-10 19:19pm
Also, designers of attribute classes should always avoid this situation. For example, if there is one constructor accepting string where empty string is allowed, there must be yet another (parameter-less) constructor that set this string slot to empty (or null, whatever is appropriate).
Rate this:
Please Sign up or sign in to vote.

Solution 1

string.empty handles memory efficently than string x="";
"" actually creates an object but string.empty does not.

Also, it is a best practise to avoid hardcoding like "";
hemantwithu 24-Sep-10 7:37am
hemantwithu 25-Sep-10 9:10am
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Sergey Alexandrovich Kryukov 20-Dec-10 19:22pm
I'm absolutely agree with your note on hard-coding "", but not with efficiency: try to call object.ReferenceEquals to compare the two -- it will return "true". I'm not sure this is guaranteed.

In nearly all cases, string.Empty is highly preferable.
Rate this:
Please Sign up or sign in to vote.

Solution 3

The most fundamental difference is missing from all previous answers.

Look at the declaration of string.Empty;

public static readonly string Empty;

So, strictly speaking this is not a constant. This is a read-only field actually assigned to a constant.
In the sample you provide with your question, "" is an immediate constant, (created every time you place it in the code).
I compared the two for referential equivalence using object.ReferenceEquals -- they appeared to be the same, but nothing really guarantee that.

The difference between a constant and read-only field can sometimes be detected during run-time. In particular, this is a reason why string.Empty is not allowed in attribute constructor where an attribute is allowed -- unfortunately! (see Toli's Answer Difference between String.Empty and ""?[^]).

In nearly all other cases, using string.Empty is allowed and highly preferred.

I usually consider mixing any kind of immediate constants (by the way, see this) with code as unacceptable, but make exclusion for code samples and other ad-hoc works.

One advice tip for not using "" in comparisons: test static predicate string.IsNullOrEmpty(string).

Another advice would be using resources instead of constants, but this is not always practical/possible. In resources, empty strings can be allowed.
Espen Harlinn 26-Feb-11 11:01am
Good effort, my 5
Sergey Alexandrovich Kryukov 26-Feb-11 19:46pm
Thank you, Espen.
It's interesting that in some article-related thread I had to proof that "" works correctly (somebody tried to argue it would create more and more unique objects every time, by object.ReferencesEqual can really test that). Nevertheless, I strongly against "" as it is actually still an unwanted immediate constant, as any other.
Rate this:
Please Sign up or sign in to vote.

Solution 5

please see the following link
Rate this:
Please Sign up or sign in to vote.

Solution 6

See this discussion[^] or
Rate this:
Please Sign up or sign in to vote.

Solution 7

The difference between string.Empty and "" is very small. String.empty will not create any object while "" will create a new object in the memory for the checking. Hence string.empty is better in memory management.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100