String intern pool and extension

Source: Internet
Author: User

In C #, if you write [csharp] string msg1 = "abc";, during compilation, it is converted to the following IL command and stored in metadata to [csharp] LdStr "abc". Then, if you define another [csharp] string msg2 = "abc ";, the compiler recognizes that "abc" already exists and calls this IL command to direct the reference msg1 to msg2, of course, if you use the new method, it must be a new instance, and this will not work. In addition, when adding or subtracting a string, because the string type is a constant reference type, it means that as long as you perform some operations on the string, a new instance is usually generated. For the string pool, string has a method [csharp] string. intrrn (string str), this method will find the string reference with the same str value in the string pool and return it. If you use this method, it can be said that it saves the memory space, but it is not good if you use it too much, because it will remain occupied and will not be released until the CLR ends, that is, it will always exist until your program ends.

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.