C # string retention mechanism and lock.

Source: Internet
Author: User

Because the C # string retention mechanism, the followingCode:

String thekey1 = "xxxxxx"; string thekey2 = "xxxxxx"; if (object. referenceequals (thekey1, thekey2) {string theC = thekey1 + thekey2 ;}

Thekey1 and thekey2 point to the same address, but the following code:

Int Thea = 1; string thekey1 = "XXX" + Thea; string thekey2 = "XXX" + Thea; If (object. referenceequals (thekey1, thekey2) {string theC = thekey1 + thekey2 ;}

Thekey1, thekey2 references are not equal. Note that the string retention mechanism of c # is only for string constants.

From the above features, in fact, it is best not to use strings, especially concatenated characters, when locking, it will be ineffective.
I originally wanted to use this stitching feature to complete layer locks of different levels, but it was ineffective after tests. Other methods were used later.

PS: the retention mechanism of the string is to find whether there is any memory for the string when allocating it. If yes, it will not be allocated and will be used directly. although this mechanism can save a certain amount of space, it will cause some performance loss. If the string is too broken, it will waste space and time. the immutability of strings facilitates parallel programming. however, it is a pity that the lock mechanism does not use the content equality of strings.

 

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.