0

Possible Duplicate:
In C#, should I use string.Empty or String.Empty or “” ?

Is it better to

return string.Empty;

instead of

return "";

What do you think?

0

3 Answers 3

7

The two are exactly identical once compiled. I find string.Empty is more readable. For all we know "" really should have something inside of it and it's just a typo.

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

1 Comment

Also, it's harder to spot a typo like "'" when you really meant the empty string "".
3

According to this MSDN blog post string.Empty is more efficient as it will not produce a new object.

Here are some more information, including a short performance comparison.

2 Comments

Yeah but it is pre .NET 2.0: stackoverflow.com/questions/151472/… ;-)
The blog post is not correct. The string literal is created only once when the assembly is loaded (just like the string object for the String.Empty property), executing the code that uses the string literal doesn't create an object.
1

Once the compiler gets done with that code, the IL/machine code for both fragments will be identical.

I tend to use string.Empty when an empty string is an important part of the algorithm. I tend to use "" for values that could be anything but for now it just happens to be an empty string. In other words, use the one that communicates the intent of the expression.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.