Learn more about string

Source: Internet
Author: User
Tags mscorlib
Today, did you really understand the string in. Net? Of Article It is a good description of the string, but I want to add some points. Once your string is created in the heap, it will exist in the memory as Const, any modification will make it re-created as a new string, and the reference pointing to the previous string will point to this new string !!

Test 1:
See the followingCode:1StringS= "1";
2Console. writeline (string. isinterned (s)! =Null); // Output True here

This code is very simple. It is named string S and assigned "1". In this case, S is represented as a reference in the CLR built-in pool. Let's look at the following code: 1 String S =   " 1 " ; // Initialize string
2 Console. writeline (string. isinterned (s) ! = Null ); // Output True here
3
4 S + =   " 2 " ; // Append string
5 Console. writeline (string. isinterned (s) ! = Null ); // Output false here
6

Now let's look at the output: S is in the built-in pool when S = "1" for the first time, but after you modify the value of S, it is no longer in the built-in pool !!
If you need to place s in the built-in pool again, you can do this: 1 String S =   " 1 " ; // Initialize string
2 Console. writeline (string. isinterned (s) ! = Null ); // Output True
3
4 S + =   " 2 " ; // Append string
5 Console. writeline (string. isinterned (s) ! = Null ); // Output false
6
7 String. Intern (s ); // Reset to reference
8 Console. writeline (string. isinterned (s) ! = Null ); // At this time, the output is still true.

Test 2:
See the following test code: 1 String A =   " 1 " ; // Built-in string for the first time
2 A + =   " 2 " ; // For the second allocation, the first value is 1 to the new address, and the memory needs to be re-allocated.
3 A + =   " 3 " ; // For the third allocation, the first two values are assigned to the new address, and the memory needs to be re-allocated.
4 A + =   " 4 " ; // For the fourth allocation, the first three values of 1, 2, 3 are assigned to the new address, and the memory needs to be re-allocated.
5
6 /**/ /* Result After decompilation using IL
7 *
8 *. Method private hidebysig static void main (string [] ARGs) cel managed
9 {
10 . Entrypoint
11 . Custom instance void [mscorlib] system. stathreadattribute:. ctor () = (01 00 00 00)
12 // Code size 43 (0x2b)
13 . Maxstack 2
14 . Locals Init ([0] string)
15 Il_0000: ldstr "1"
16 Il_0005: stloc.0
17 Il_0006: ldloc.0
18 Il_0007: ldstr "2"
19 Il_000c: Call string [mscorlib] system. String: Concat (string ,//Note that copy once
20 String)
21 Il_0011: stloc.0
22 Il_0012: ldloc.0
23 Il_0013: ldstr "3"
24 Il_0018: Call string [mscorlib] system. String: Concat (string ,//Secondary Replication
25 String)
26 Il_001d: stloc.0
27 Il_001e: ldloc.0
28 Il_001f: ldstr "4"
29 Il_0024: Call string [mscorlib] system. String: Concat (string ,//Copy for the third time
30 String)
31 Il_0029: stloc.0
32 Il_002a: Ret
33 } // End of method class1: Main
34 *
35

I believe that through the above Code and the decompiled results, you can see clearly how the string is allocated, and how it works when you modify the string again .. now that the problem is found, the problem can also be solved .. there are many solutions and optimization methods. I only list several simple methods. The first one is replaced by the string [] array .. see the following code:

1 String [] Arr1 =   New   String [ 4 ]; // Four built-in strings are required.
2 Arr1 [ 0 ] =   " 1 " ; // Built-in 1
3 Arr1 [ 1 ] =   " 2 " ; // Built-in 2
4 Arr1 [ 2 ] =   " 3 " ; // Built-in 3
5 Arr1 [ 3 ] =   " 4 " ; // Built-in 4
6 /**/ /* Representation after Il decompilation after array assignment
7 *
8 *
9 *. Method private hidebysig static void main (string [] ARGs) cel managed
10 {
11 . Entrypoint
12 . Custom instance void [mscorlib] system. stathreadattribute:. ctor () = (01 00 00 00)
13 // Code size 40 (0x28)
14 . Maxstack 3
15 . Locals Init ([0] string [] arr1)
16 Il_0000: LDC. i4.4
17 Il_0001: newarr [mscorlib] system. String
18 Il_0006: stloc.0
19 Il_0007: ldloc.0
20 Il_0008: LDC. i4.0
21 Il_0009: ldstr "1"
22 Il_000e: stelem. Ref
23 Il_000f: ldloc.0
24 Il_0010: LDC. i4.1
25 Il_0011: ldstr "2"
26 Il_0016: stelem. Ref
27 Il_0017: ldloc.0
28 Il_0018: LDC. i4.2
29 Il_0019: ldstr "3"
30 Il_001e: stelem. Ref
31 Il_001f: ldloc.0
32 Il_0020: LDC. i4.3
33 Il_0021: ldstr "4"
34 Il_0026: stelem. Ref
35 Il_0027: Ret
36 } // End of method class1: Main
37 *
38 * */

Look at the code above. I want to understand how string [] works ?? It can be understood that the subitem of each array is a built-in string !!
The second solution is Char []. If you know the size of your string, you can write char [] C = new char [4] {'1', '2 ', '3', '4'}; this method is very good in C/C ++, but it does not seem to be used much in C #, And it is troublesome to use it. because it cannot think of C/C ++ like this: Char [] C = {"1234 "}; however, I still use char [] When doing an encryption/Decryption class.!
The last and most commonly used method is stringbuilder. Since string has the side effect of address reallocation, Microsoft also provides stringbuilder to solve this problem ..
Due to space limitations, I will describe the implementation and skills of stringbuilder in the next space. After the release, I will connect it here ..
Implementation and skills of stringbuilder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.