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)