0

Possible Duplicate:
What is the difference between String.Empty and “”
In C#, should I use string.Empty or String.Empty or “”?
Default string initialization: NULL or Empty?

I would like to know if there is a interest to initialize a string?

String value1 = String.Empty;

EDIT : My question is more Why should I initialize strings than choosing between "" or string.Empty

Community
  • 1
  • 1
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200

3 Answers3

4

IMO, use string.Empty since it increases code readability. Code style scanners like Gendarme and FxCop will flag usage of "" as against best practice.

Edit: Removed blurb about memory usage, which was incorrect.

jmacinnes
  • 1,589
  • 1
  • 11
  • 21
2

Assuming the question means "Why should you initialize strings" and not "Which method is best for initializing strings"...

Yes, it is a good idea to initialize strings to minimize the potential for null reference exceptions in your code. It is easy to create a code path where the string is never set and when you call a method on that string it will error.

JohnFx
  • 34,542
  • 18
  • 104
  • 162
1

Considering that there is a meaningful difference between a null string and and empty string, I think that the answer is "it depends". If your code has no use for the null value, and you want to be able to write code referencing the variable with the assumption that the value is non-null then it makes sense to initialize. But there may be cases where null is valid and useful, and initializing does not make sense. For instance, if the string holds a value received from some test equipment, null may convey that no value was received, while "" conveys that something was received, and it happened to be an empty string.