Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between String.Empty and "" (empty string)?

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?

like image 325
johnc Avatar asked Sep 30 '08 01:09

johnc


People also ask

What is difference between null string and empty string in C#?

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.

Is string empty the same as in C#?

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, "".

Does string empty equal?

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.

What is empty string used for?

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.


1 Answers

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.

like image 194
Brian R. Bondy Avatar answered Oct 01 '22 19:10

Brian R. Bondy