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;
String.Empty
and string.Empty
are equivalent. String
is the BCL class name; string
is its C# alias (or shortcut, if you will). Same as with Int32
and int
. See the docs for more examples.
As far as ""
is concerned, I'm not really sure.
Personally, I always use string.Empty
.
Just about every developer out there will know what "" means. I personally encountered String.Empty the first time and had to spend some time searching google to figure out if they really are the exact same thing.
Any of the above.
There are many, many better things to pontificate. Such as what colour bark suits a tree best, I think vague brown with tinges of dulcet moss.
I think the second is "proper," but to be honest I don't think it will matter. The compiler should be smart enough to compile any of those to the exact same bytecode. I use "" myself.
string
is synonym for System.String
type, They are identical.
Values are also identical: string.Empty == String.Empty == ""
I would not use character constant "" in code, rather string.Empty
or String.Empty
- easier to see what programmer meant.
Between string
and String
I like lower case string
more just because I used to work with Delphi for lot of years and Delphi style is lowercase string
.
So, if I was your boss, you would be writing string.Empty
I use the third, but of the other two the first seems less odd. string is an alias for String, but seeing them across an assignment feels off.