27: Use string type (2)

Source: Internet
Author: User

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.

 

 

 

 

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.