In .NET, what is the difference between String.Empty
and ""
, and are they interchangable, or is there some underlying reference or Localization issues around equality that String.Empty
will ensure are not a problem?
String. Empty because it is a static variable, rather than "" which has to create a new string, and null means that you must then set the string equal to a new instance of a string.
It's not different. http://msdn.microsoft.com/en-us/library/system.string.empty.aspx: The value of this field is the zero-length string, "".
An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.
The empty string is a legitimate string, upon which most string operations should work. Some languages treat some or all of the following in similar ways: empty strings, null references, the integer 0, the floating point number 0, the Boolean value false, the ASCII character NUL, or other such values.
In .NET prior to version 2.0, ""
creates an object while string.Empty
creates no objectref, which makes string.Empty
more efficient.
In version 2.0 and later of .NET, all occurrences of ""
refer to the same string literal, which means ""
is equivalent to .Empty
, but still not as fast as .Length == 0
.
.Length == 0
is the fastest option, but .Empty
makes for slightly cleaner code.
See the .NET specification for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With