Before the early morning, there is a very interesting post (String reference type, or a value type), which leads to a warm discussion. The collection was given. These days, I have no sound, when I walked on the favorites, I saw it, I feel very interesting and studied. Have a little bit. First, String is a reference type, which is definitely not to be doubtful. String is inherited from the System.Object type, not like int32, inherits from System.ValeType. But String is also a more strange type. In general, a reference type, a configuration, is called IL instruction: newobj. But string has its own special IL instruction: ldstr. This description, although String is also a reference type, it is still different from it and other reference types. Specifically, the CLR uses a string resident technology to enhance the String performance. Simply put, string resident technical guarantee: the same string of content, which is the same in the hosted stack, so that the existence of a large amount of the same String instance is avoided, thereby increasing the performance of the program, below The code can verify this. String s = "1112"; messagebox.show (Object.referenceeequals (s, "1112"). TOSTRING ()); the result is true. In addition, the string, there is also a feature, which is constant. QQCHEN also discussed this. Let's take a look at the code below. Unsafe {
String s = "123"; string b = "123"; fixed (char * p = s) {system.intptr p (system.intptr) p; console.writeline ("This is the initial address of S, {0} ", ip.tostring ());} fixed (char * p = b) {system.intptr i (system.intptr) P; console.writeline (" This is the address of B, {0} ", ip.tostring ());} S = "234"; fixed (char * p = s) {system.intptr ip = (system.intptr) P; console.writeline ("This is the modified address of S, {0}", ip.tostring ());} fixed (char * p = b) {system.intptr i (system.intptr) p; console.writeline ("This is the address after s modification, {0}", IP .Tostring ());
} From the above code, we are not difficult to see, S and B, when the address is the same, after modifying S, the address of S changes, but B is constant. This is the concrete manifestation of String's constant constant and string resident. I mainly want to say, String as a question of parameter transmission. Similar to all reference types, String is transmitted as a parameter transmission, passing the copy of the address, just like the value copy of the value type type. Sometimes, there may be such a confusion, since the string is passed according to the address, then I have modified String inside the method, why is the external String not affected? For example, the following code: String s = "initial value"; public void change "{varstr =" Modified! "After calling ChangeStr (s), S's content is still" initial value ", this is why? The reason is: Although the address of Varstr and s starts, it is consistent, but after the content of modifying Varstr, their address is inconsistent! This can be reflected in the code above. We still use the code to verify: Unsafe {string s = "123"; fixed (char * p = s) {system.intptr i (system.intptr) P; console.writeline ("This is the initial address of S, { 0} ", ip.tostring ());} ChangeDSTR (s);} public void changestr (string s) {unsafe {fixed (char * p = s) {system.intptr p (system.intptr) P; console .Writeline ("This is the initial address, {0}", ip.tostring ());} s = "134"; fixed (char * p = s) {system.intptr ip = (System.intptr) p; console.writeline ("This is the address, {0}", {0} ", {0} ());}
} The results of the above code, yes, the address of the modified parameter S is different from the address that is passed. In this way, we will understand that [after calling ChangeStr (s), S's content is still "initial value", why? 】this problem. If we have to get this modified result, then you can only use REF to modify the rule. Public Void Changedstr (Ref string s) so that the modified value can be obtained.