concept
- Reference type
- Once created, it cannot be changed
- collection of char structures
characteristic
Consistency: Once a string is created, it will not change, and any operation will generate a new string.
Persistence: The same string is created only once in the heap, and objects with the same string content refer to an address
The basic principle of string residency
- When the CLR is initialized, it will create a resident pool in the memory, and the internal is a hash table, which stores the string and its memory address
- When assigning a string, first look for it in the resident pool, and if found, return the address. If not, create a new string and store it in the resident pool
- Residency is thread-level, shared by multiple APPdomains, and is not controlled by CG
- Only through the IL instruction ldstr string can reside
- String.Interned(): Resides the specified string
- String.IsInterned(): Check whether the string is resident
StringBuilder
Capacity: the capacity of the string
Length: the length of the string
will not create a large number of new objects
The situation of creating a new object:
- StringBuilder.ToString(),
- When appending a string, if it exceeds the currently set capacity, a larger capacity character array will be created