Special string type, special string type

Source: Internet
Author: User

Special string type, special string type
1. Preface

String belongs to the reference type. Do you know this? However, in the process of usage, we find that it still has some value-type features. Why?

The reason is that. Net considers that when a large number of operations are performed on string objects and a large number of operations are performed on referenced objects, the performance is certainly not as good as the value type .. To improve this performance, Net provides a dedicated solution: String resident pool!

 

2. Text

Let's take a look at the Code:

1             string str1 = "aa";2             string str2 = "a" + "a";3             Console.WriteLine(ReferenceEquals(str1, str2));  //print:true

The str1 and str2 memory point addresses are exactly the same!

The reason is. net maintains a Hash table in the CLR (in fact, the string resident pool mentioned above). The key is the string content and the value is the address of the managed heap; when a new string is created during initialization ,. net will search for the same value in the Hash table. If the key is the same, the address value of the existing string will be assigned to the newly created string, if the Code does not exist, the address is reassigned, which is why the memory in the code above is true.

 

Let's look at another piece of code:

1             string str3 = "ab";2             string str4 = "a";3             str4 += "b";4             Console.WriteLine(ReferenceEquals(str3, str4));//print :false

When the keyword "initialize creation" in the previous column is false, when the string is dynamically created,. Net does not search for creation in the Hash table, but directly creates the string;

 

If you want to optimize the above Code and have a higher performance (xian) high (de) catch (dan) Evaluate (teng ), we can manually add this string to the string resident pool for comparison.

1 string str3 = "AB"; 2 string str4 = "a"; 3 str4 + = "B"; 4 str4 = string. intern (str4); // Intern: it will go to the string resident pool for search. if it finds it, it will return the corresponding address 5 Console. writeLine (ReferenceEquals (str3, str4); // print: true

 

3. Summary

Conclusion:

1. string is not created using the newobj command in clr, but created using the ldstr command! In addition, string has the value type feature, but it is a reference type in the memory and exists in the managed heap;
2. string is modified by sealed, so it cannot be integrated with quilt classes;
3. When the created content is the same, string points to the same address, and a new address is generated for each string operation (the constant of string );
4. For a large number of Concatenated strings, we still use StringBuilder. It is dynamic and not constant as the string, but it is costly to create StringBuilder. Therefore, the performance of small concatenated strings may be better!

 

Finally, there is a small advertisement for selling points: recruitment in Shenzhen. net development engineers (for medical care), interested partners can send resumes (after the job, we will take the recommendation bonus to the popular hot pot of Merlin to eat it ~)

 

Related Article

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.