String.Empty vs null. What are you using?

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

He says don't do it

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

Does it really matter? I know that a string is an object, so it makes sense.

I know this is a stupid question, but what is your opinion?

+66
string null c #
Jul 14 2018-11-11T00:
source share
5 answers

null and Empty very different, and I do not suggest arbitrarily switching between them. But they don’t have an extra “cost”, because Empty is the only fixed link (you can use it as many times as you like).

There is no "pollution" on the stack caused by ldsfld - this problem ... is crazy. Downloading null may be slightly cheaper, but may result in null-reference exceptions if you are not careful when checking the value.

Personally, I don't use either ... If I want an empty string, I use "" - simple and straightforward. Interning means it also has no 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. Moreover, in later versions of .NET, JIT has a direct interception of this data, which gives an empty a link to a string without actually performing a static field search. Basically, there is no reason to care in any case other than readability. I just use "".

+89
Jul 14 '11 at 7:35
source share

It doesn’t "pollute the stack", there is no technical reason, but there is a big difference between setting a variable to an object reference (even if it is an empty string) and null . They are not the same 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 do not know what is most suitable?

Edit, added examples:

  • You can use string.Empty as the default postfix for a person’s name (most people do not have PhD, for example)

  • You can use null for a configuration parameter that was not specified in the configuration file. In this case, string.Empty will be used if the configuration parameter is present, but the required configured value was empty.

+23
Jul 14 2018-11-11T00:
source share

They are different, as others have already answered.

 static void Main(string[] args) { string s1 = null; string s2 = string.Empty; string s3 = ""; Console.WriteLine(s1 == s2); Console.WriteLine(s1 == s3); Console.WriteLine(s2 == s3); } results: false - since null is different from string.empty false - since null is different from "" true - since "" is same as string.empty 

The problem of managing an empty line with respect to zero lines becomes a problem when you need to either transfer it to a flat file or transfer it through a link. Therefore, I believe that it may be useful for others who visit this page to give a good solution to this particular problem.

To save lines to a file or link:
you probably want to convert the string to bytes.
a good recommendation that I recommend is to add 2 segments of header bytes to your converted string.

segment 1 - meta-information that is stored in 1 byte and describes the length of the next segment.

segment 2 - contains the length of the string that you want to save.

Example:
string "abcd" - for simplicity, I will convert it using an ASCII encoder and get {65,66,67,68}.
compute segment 2 will give 4 - so 4 bytes is the length of the converted string.
compute segment 1 will give 1 - since only 1 byte was used to store information about the length of the converted string information (which was 4, that is, if it were 260, I would get 2)

The new byte strip will now be {1,4,65,66,67,68}, which can be saved in a file.

The advantage with respect to the subject is that if I had an empty string to save, I would get an empty byte array of length 0 from the conversion and after calculating the segments, I end up with {1,0} which can be saved, and then load and interpret back to the empty string. On the other hand, if I had a null value in my line, I would just get {0}, because my array of bytes to save and again when the load can be interpreted to a null value.

There are more advantages, such as knowing what size will load or accumulate if you use multiple lines.

Let's go back to the topic - it will be ... a good kind of polluting stack, since the same described principles are used by some system to distinguish between zeros and empty ones .. so yes string.Empty takes up more memory than null, although I didn’t name it would be pollution .. it's just 1 byte.

+4
May 7, '13 at 21:58
source share

He was answered to death, but null means value, not initialized. string.Empty means "" (empty string) as specified on MSDN.

The safest way to check for an empty or null string is to use string.IsNullOrEmpty.

0
Sep 27 '16 at 21:07
source share

FWIW, I found that mixing "" and String.Empty does not work:

 var a = ""; alert("a " + (a == "") + ", " + (a==String.Empty)); //Yields "a true, false" var b = String.Empty; alert("b " + (b == "") + ", " + (b == String.Empty)); //Yields "b false, true" 

In particular, if you use $.trim to get the value of an empty DOM input field, compare it to String.Empty , you will get false . I don’t know why this is so, but you go there. Now I just use "" everywhere for consistency.

-one
Feb 07 '12 at 23:52
source share



All Articles