Comparison between String. Equals (object, object) and (object = object) in C #

Source: Internet
Author: User

Scenario 1
String s = "Test ";
String t = string. Copy (s );
Console. WriteLine (s = t );
Console. WriteLine (object) s = (object) t );
The output is true and false.

Case 2
String s = "Test ";
String t = s;
Console. WriteLine (s = t );
Console. WriteLine (object) s = (object) t );
True for output

Case 3
String s = "Test ";
String t = string. Copy (s );
Console. WriteLine (s. Equals (t); // true
Console. WriteLine (object) s. Equals (object) t); // true

Case 4
String s = "Test ";
String t = s;
Console. WriteLine (s. Equals (t); // true
Console. WriteLine (object) s. Equals (object) t); // true

Conclusion:
String. Equals (); Method (inherited from its superclass Object by the String class) is used to detect whether two objects are equal, that is, whether the content of the two objects is equal.
= It has different functions for comparing and referencing basic data types:
Compares the basic data types. If the two values are the same, the result is true.
When comparing references, if the reference points to the same object in the memory (that is, the memory address is the same), the result is true.

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.