In C#, I want to initialize a string value with an empty string.
How should I do this? What is the right way, and why?
string willi = string.Empty;
This topic is pretty old and long, so excuse me if this behavior has been mentioned somewhere else. (And point me to the answer that covers this)
I have found a difference in the behavior of the compiler if you use string.Empty
or double quotes. The difference shows itself if you don't use the string variable initialized with string.Empty or with double quotes.
In case of initialization with string.Empty
then the Compiler Warning
CS0219 - The variable 'x' is assigned but its value is never used
is never emitted while in case of initialization with double quotes you get the expected message.
This behavior is explained in the Connect article at this link: https://connect.microsoft.com/VisualStudio/feedback/details/799810/c-warning-cs0219-not-reported-when-assign-non-constant-value
Basically, if I get it right, they want to allow a programmer to set a variable with the return value of a function for debugging purposes without bothering him with a warning message and thus they limited the warning only in case of costant assignments and string.Empty is not a constant but a field.