String.intern Method (String)

Source: Internet
Author: User

public static string Intern (String str)

Parameters
Str

The string to search for in the staging pool.

return value type:System.String

Returns a reference to the system if stris staged, otherwise returns a new reference to a string with a value of str .

Introduction of a string detention pool

     in a program, if you assign the same string to a different string reference, the system allocates memory space multiple times, but the same string is stored in the memory (see). This not only wastes valuable memory, but also greatly reduces the performance of the system. To improve the performance of the program,. NET proposes a string detention pool mechanism to address this problem. i.e. So, there is an instance with a literal string of a particular value only once in the system.

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the staging pool and assigns it to each variable.

String a= "ABCDE"; String b= "ABCDE";

Ii. principles of the string detention pool

Once a string detention pool is used, a container is created internally when the CLR starts, and the container's key is the string content, and the value is a reference to the string on the managed heap. When a new string object needs to be allocated, the CLR first detects whether the string object is contained in the container, returns a reference to the string object that already exists if it is already included, and if it does not, a new string object is assigned and added to the inner container to return the reference to the string object. However, the string detention pool mechanism no longer works when a string object is assigned with a declaration that is displayed with the New keyword.

  The Intern method uses a staging pool to search for a string with a value equal to str. if such a string exists, it returns a reference to it in the staging pool. If the string does not exist, a reference to str is added to the staging pool, and the reference is returned.

Other:

public static string isinterned (string s);

The function is similar to the previous method. Just when the string s is not in the string detention pool, new objects are not allocated and NULL is returned.

  

String.intern Method (String)

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.