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;
or
string willi = String.Empty;
or
string willi = "";
or what?
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;
or
string willi = String.Empty;
or
string willi = "";
or what?
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 Warningis 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.
string
is synonym forSystem.String
type, They are identical.Values are also identical:
string.Empty == String.Empty == ""
I would not use character constant "" in code, rather
string.Empty
orString.Empty
- easier to see what programmer meant.Between
string
andString
I like lower casestring
more just because I used to work with Delphi for lot of years and Delphi style is lowercasestring
.So, if I was your boss, you would be writing
string.Empty
On http://blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx :
I wasn't going to chime in, but I'm seeing some wrong info getting tossed out here.
I, personally, prefer
string.Empty
. That's a personal preference, and I bend to the will of whatever team I work with on a case-by-case basis.As some others have mentioned, there is no difference at all between
string.Empty
andString.Empty
.Additionally, and this is a little known fact, using "" is perfectly acceptable. Every instance of "" will, in other environments, create an object. However, .NET interns its strings, so future instances will pull the same immutable string from the intern pool, and any performance hit will be negligible. Source: Brad Abrams.
I would favor
string.Empty
overString.Empty
because you can use it without needing to include ausing System;
in your file.As for the picking
""
overstring.Empty
, it is personal preference and should be decided by your team.I'd prefer
string
toString
. choosingstring.Empty
over""
is a matter of choosing one and sticking with it. Advantage of usingstring.Empty
is it is very obvious what you mean, and you don't accidentally copy over non-printable characters like"\x003"
in your""
.