The string object is immutable. That is to say, the string cannot be changed after it is created. There are several advantages to immutable the string. First, it allows you to perform various operations on a string without actually changing the string, as shown below:
If(S. toupperinvariant (). substring (10, 20). endswith (". EXE"))
{
//...
}
Here, Toupperinvariant returns a new string without modifying the string S character. In Toupperinvariant The Substring The Operation also returns a new string. Endswith Check. Toupperinvariant , Substring The two temporary strings created are notProgramFor long reference, the garbage collector will recycle their memory later. If you perform a large number of string operations, you can create a large number of string objects in the heap, resulting in more frequent garbage collection, which hinders the application's hate. To efficiently execute a large number of string operations, it is best to use the stringbuilder class.
Changing the string will not cause thread synchronization issues when you operate or access a string. In addition, CLR can share multiple completely consistent contents through a single string object, this reduces the number of strings in the system and saves memory.