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