String = "" string = empty string = null, stringemptynull
I. I think it is the same as string. Empty. There is an article on the Internet that has been reposted dozens of times. This is a 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. And string. empty is a c # syntax-level optimization. It is optimized when the C # compiler compiles the code into IL (MSIL, that is, all accesses to the static field Empty of the string class are 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 is, string. 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
Reprinted from: http://www.bitscn.com/pdb/dotnet/201003/181883.html
The best way to judge whether the string is null is s. Length = 0!
Several methods for determining as a Null String, in the order of performance from high to low:
S. Length = 0 is better than s = string. Empty is better than s = ""
Memory monitoring window
What is the difference between null and stringEmpty in C #?
When you define only one str without allocating memory for it, use string str = null, however, before using it, you must assign a value for it, such as str = "xxxx", that is, initialize it.
String str = string. Empty is defined and initialized together. It is equivalent to string str = null; str = "";
For specific use, if you are sure to assign values to str before use, use string str = null; otherwise, use string str = string. Empty; otherwise, a null pointer exception may occur during program running.
For String initialization, use = "", = null, = StringUtilsEMPTY. Which one is better?
1. string str = "" is equivalent to string str = string. empty
2. If you only define a string variable without allocating memory to it, use string str = null.
In this case, you will assign a value to str in the next operation.
3. string str = null; str = "" is equivalent to string str = ""