A string is a very special type of data that is both a primitive type and a reference type, at compile and run-time. NET has done some optimization work on it, formal these optimizations sometimes confuse programmers, making string look elusive, this article is divided into two chapters, a total of four sections, to tell about the strange side of string.
A A constant string
To get a more comprehensive understanding of the STIRNG type, you need to be clear first. The value type and reference type in net. In C #, the following data types are value types:
BOOL, Byte, char, enum, sbyte, and numeric types (including nullable types)
The following data types are reference types:
class、interface、delegate、object、stirng
See, the stirng we're going to talk about is impressively. Being declared as a string variable is stored in the heap and is an out-and-out reference type.
So many students have questions about the following code, is the string type "far-reaching"? Let's take a look at the following three lines of code:
string a = "str_1";
string b = a;
a = "str_2";
Do not say boring, this point must be clear! In the above code, the "=" in line 3rd has a hidden secret: its effect can be understood as a new one, not a modification of the variable "a". Here's the IL code to illustrate this:
.maxstack 1
.locals init ([0] string a,
[1] string b)
IL_0000: nop
IL_0001: ldstr "str_1"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: stloc.1
IL_0009: ldstr "str_2"
IL_000e: stloc.0 //以上2行对应 C#代码 a = "str_2";
IL_0015: ret
You can see the 1th, 6 lines of the IL code, creating the string "str_1" by the LDSTR directive and associating it with the variable "a"; 7, 8 rows immediately eject the value on the top of the stack and correlate it to the variable "B"; 9, 10 creates the string "Str_2" by Ldstr and associates it with the variable "a" (not to modify the old value of variable A, as we imagined, but to produce a new string);
In C #, if you instantiate a class with the new keyword, the corresponding is done by the IL instruction newobj, and a string is created by the ldstr instruction, and you see the ldstr instruction, we can assume that IL wants to create a new string. (Note: Il wants to create a string that is ultimately created and is determined at run time by the mechanism at which the string resides, as described in the following sections.) )
So, the third line of C # code (A = "str_2";) looks like it is modifying the old value "str_1" of variable A, but it actually creates a new string "str_2" and then points the pointer to the memory address of "str_2" and "str_1" is still not affected in memory, so the value of variable B does not change---this is the constant of string, students, we must keep this in mind, in. NET, objects of type string cannot be modified once they are created! Operations, including ToUpper, SubString, and trim, generate new strings in memory.
This section focuses on the review: Because of the constant nature of the stirng type, it is often misunderstood that a string is a reference type but often shows the attribute of a value, because it is not understood that the constant of string is caused by a "value attribute". For example:
string a = "str_1";
a = "str_2";
This creates "str_1" and "str_2" two strings in memory, but only "str_2" is in use, and "str_1" is not modified or disappeared, thus wasting memory resources, which is why it is recommended to use StringBuilder when doing a large number of string operations.