This is a common problem that has been discussed on the Internet. However, I don't think it is a perfect solution. Some of them are misleading others. Next, let me explain my understanding of the three. If there are any errors, please correct them in time.
I. I think it is the same as string. Empty. One article on the Internet has been reposted dozens of times.ArticleIn this case, String. Empty does not allocate a bucket. "" an empty bucket is allocated. I think this statement is incorrect and ambiguous.
1. In fact, empty is a static read-only field in the string class. Its definition is as follows:
Public static readonly string empty = "";
That is to say, the internal implementation of string. Empty is equal.
2. I want to refute the argument that string. Empty does not allocate a bucket. "" assign a bucket with an empty length. First, both string. Empty and "" will allocate storage space. Specifically, the storage space will be allocated on the memory stack and stack.
The reference type is to save the actual data of an object on the stack and save the address of the object on the stack. Therefore, String. empty and "" both store an address on the stack. This address occupies 4 bytes and points to a space with a length of 0 in the memory heap. This space is saved as string. the actual value of empty. I can use vs2010 to track the memory for demonstration.
0x01e81228 in is the address where the variable STR is stored in the stack.
For "", see
This figure has the same effect, that is, "" also saves an address on the stack.
3. CLR will optimize the string, so "" And string. Empty will also be optimized.
Declare the following two variables
String str1 = "";
String str2 = "";
The references of str1 and str2 are the same, that is, the addresses stored on the stack of str1 and str2 are the same. See
Is the str1 address. Is the str2 address.
It can be seen that the addresses of str1 and str2 are the same, that is to say, str1 will occupy a space with a length of 0 on the stack, while str2 will not open up new space on the stack, str2 and str1 share the same space on the stack.
Similarly,
String str3 = string. empty;
String str4 = string. empty;
The addresses of str3 and str4 are the same. It also shares the same space on the memory stack. For example
Is the str3 address. Is the str4 address.
4. If you have to say "" is different from string. empty, I think 1 is written differently, and string. Empty looks nice ~!~. 2. There is a slight difference in optimization. String. Empty is optimized at the syntax level in C. This can be seen through the internal implementation of string. Empty.
Public static readonly string empty = "";
That is to say, "" is optimized through CLR. CLR maintains a string pool to prevent duplicate strings from being created in the heap. String. Empty is a C # syntax-level optimization.CodeIt is optimized when compiled into il (msil), that is, All accesses to the static field empty of the string class will be directed to the same reference to save memory space.
Therefore, the "" optimization is more dependent on CLR.
Let's take a look at the compiled il code:
String str1 = "; compiled as follows:
Ldstr "" // extract a "from the string pool (actually the address is used)
Stfld string classlibrary1.class1: str1 // assign "" To str1 (actually the address is assigned)
String str2 = string. Empty; compiled as follows:
Lds1_string [mscorlib] system. String: Empty // obtain the static field empty of the string class (actually the address is used)
Stfld string classlibrary1.class1: str2 // assign empty to str2 (actually the address is assigned)
Summary: I think it is a big push, and it may be hard for beginners to understand. I am not good at Chinese literature. I 'd like to ask for your understanding about the ability to express myself.
"" Is basically the same as string. Empty in terms of usage and performance. String. Empty is a syntax-level "optimization.
Ii. Differences between string. Empty and null
Because string. Empty and "" are basically the same, the difference between string. Empty and null represents the difference between "" and null.
1. that isString. Empty occupies a space of 0 on the heap, but null does not. The details are as follows:
String str1 = "";
String str2 = NULL;
As mentioned above, str1 will save an address on the stack, which occupies 4 bytes and points to a space with a length of 0 in the memory heap. This space stores the actual value of str1.
Str2 will also save an address on the stack, which also occupies 4 bytes, but this address is not explicitly pointed to, and it does not refer to any, its content is 0x00000000. For example