C # string retention mechanism and Lock.

Source: Internet
Author: User

Because C #'s string retention mechanism, the following code: [csharp] string theKey1 = "XXXXXX"; string theKey2 = "XXXXXX"; if (object. referenceEquals (theKey1, theKey2) {string theC = theKey1 + theKey2;} string theKey1 = "XXXXXX"; string theKey2 = "XXXXXX"; if (object. referenceEquals (theKey1, theKey2) {string theC = theKey1 + theKey2;} theKey1, theKey2 points to the same address. but the following code: [csharp] int theA = 1; string theKey1 = "XXX" + theA; string theKey2 = "XXX" + theA; if (object. referenceEquals (theKey1, theKey2) {string theC = theKey1 + theKey2;} int theA = 1; string theKey1 = "XXX" + theA; string theKey2 = "XXX" + theA; if (object. referenceEquals (theKey1, theKey2) {string theC = theKey1 + theKey2;} In theKey1, theKey2 references are not equal. note that the string retention mechanism of c # is only applicable to 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 are used later.

Related Article

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.