string in the original C #. Empty, "", the difference between null
One, String. Empty and ""
1. Empty is a static read-only field in the String class, which is defined as:
Represents the empty string. This field is read-only. public static readonly string Empty;
This means string. The internal implementation of Empty is equal to "". The two are slightly different in terms of optimization, string. Empty is an optimization of C # for "" at the syntax level. This can be from the above string. The internal implementation of Empty is seen. That is, "" is optimized through the CLR (Common Language Runtime), and the CLR maintains a pool of strings in case a duplicate string is created in the heap. and string. Empty is a C # syntax-level optimization that is optimized when the C # compiler compiles code to IL (that is, MSIL), that is, all access to the static fields of the string class will be pointed to the same reference to save memory space.
PS:MSIL (Microsoft intermediate Language (MSIL) Microsoft intermediate language).
2. Data of reference type saves the address of the object on the heap to " " allocates storage space, specifically allocating storage space on the stack and heap of memory. So string. Empty and "" will save an address on the stack, which occupies 4 bytes, pointing to a space in the memory heap that is 0 in length, and this space holds a string. The actual value of empty.
The test code is as follows:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace String_empty { class program { static void Main (string[] args) { stringtest (); } Public static void Stringtest () { string str; string str1; String str2; string Str3; str = string. Empty; STR1 = string. Empty; STR2 = ""; STR3 = "";}} }
after setting a breakpoint, the result is as follows:
Can be found in this notation, string. The address of Empty and "" is the same.
Because of string. Empty is defined as a static readonly, and according to the results of the above operation, string. Empty does not apply for new memory, but every time it points to a fixed static read-only memory area, "" is also the same.
String. Empty and "" are basically no different in usage and performance. string. Empty is an optimization of "" at the syntax level .
Two, String. The difference between Empty and "" and null
The code is as follows:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace String_empty { class program { static void Main (string[] args) { stringtest (); } public static void Stringtest () { string str; string str1; String str2; str = string. Empty; STR1 = ""; str2 = null;}} }
After setting a breakpoint, the result is as follows:
As you can see from the running result, string. Empty and "" are allocated space on both the stack and the heap, and Null allocates only space on the stack, no allocation on the heap, or the variable does not reference any objects in memory.
string in C #. Empty, "", the difference between null