Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.Empty vs null.Which one do you use?

Tags:

string

c#

.net

null

Recently a colleague at work told me not to use string.Empty when setting a string variable but use null as it pollutes the stack?

He says don't do

string myString=string.Empty; but do string mystring=null;

Does it really matter? I know string is an object so it sort of makes sense.

I know is a silly question but what is your view?

like image 577
user712923 Avatar asked Jul 14 '11 07:07

user712923


People also ask

Should I use empty string or NULL?

A string refers to a character's sequence. Sometimes strings can be empty or NULL. The difference is that NULL is used to refer to nothing. However, an empty string is used to point to a unique string with zero length.

Should I use string empty?

"" is always a new object, which may have to be removed by the garbage collector. Therefore you should always use string. empty instead of "". The compiler will "link" them all to an empty string.

IS NULL same as empty?

The main difference between null and empty is that the null is used to refer to nothing while empty is used to refer to a unique string with zero length. A String refers to a sequence of characters. For example, “programming” is a String.

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.


2 Answers

null and Empty are very different, and I don't suggest arbitrarily switching between them. But neither has any extra "cost", since Empty is a single fixed reference (you can use it any number of times).

There is no "pollution" on the stack caused by a ldsfld - that concern is.... crazy. Loading a null is arguably marginally cheaper, but could cause null-reference exceptions if you aren't careful about checking the value.

Personally, I use neither... If I want an empty string I use "" - simple and obvious. Interning means this also has no per-usage overhead.


At the IL level, the difference here between "" and Empty is just ldstr vs ldsfld - but both give the same single interned string reference. Furthermore, in more recent .NET versions the JIT has direct interception of these, yielding the empty string reference without actually doing a static field lookup. Basically, there is exactly no reason to care either way, except readability. I just use "".

like image 163
Marc Gravell Avatar answered Oct 10 '22 06:10

Marc Gravell


It doesn't 'pollute the stack', there's no technical reason but there is a big difference between setting a variable to a reference to an object (even if it's an empty string) and null. They are not the same thing and should be used in different ways.

null should be used to indicate the absence of data, string.Empty (or "") to indicate the presence of data, in fact some empty text. Is there a specific case where you're not sure what is the most appropriate?

Edit, added examples:

  • You might use string.Empty as the default postfix for a person's name (most people don't have PhD for example)

  • You might use null for a configuration option that wasn't specified in the config file. In this case, string.Empty would be used if the config option was present, but the desired configured value was an empty string.

like image 24
Kieren Johnstone Avatar answered Oct 10 '22 06:10

Kieren Johnstone