Treat differently = and Equals

Source: Internet
Author: User

In CLR, "Equality" is divided into two types: 1. Equality of values: the two variables contain equal values. 2. Reference equality: the two variables reference the same object in the memory. However, not all types are compared by themselves. For example, string is a special reference type, but in FCL, the string comparison is overloaded to compare the "type value", rather than the comparison of "reference itself. For a custom type, if you want to compare the values rather than reference the comparison, You need to overload the Equals method. For example, for the Person class, if the IDCode is the same, we can think of them as the same person. Copy the code class Person {public string IDCode {get; private set;} public Person (string idCode) {this. IDCode = idCode;} public override bool Equals (object obj) {return IDCode = (obj as Person ). IDCode ;}} copy the code and compare it using Equals. Then, the reload method is used. Object a = new Person ("ABC"); object B = new Person ("ABC"); Console. writeLine (a = B); // False Console. writeLine (. equals (B); // True when it comes to this, the author still does not say the difference between "=" and "Equals", but just says a suggestion: "For the reference type, to define the equality of values, we should just overload the Equals method and let = indicate referencing equality ". In addition, FCL provides the Object. ReferenceEquals method to make it clear that there is a way to compare the reference equality. Bool equal = object. ReferenceEquals (object a, object B); Foreign Affairs cannot decide to ask Google, Internal Affairs cannot depend on Decompilation, and MSDN. To understand the differences between = and Equals, I will collect the following: 1. = is an operator, while Equals is a method. 2. For value type and string type, = and Equals are equal to the comparison values. Use ILSpy to decompile the Int type and observe it. the internal logic of the Equals method in the int type is "="; // int [_ DynamicallyInvokable, targetedPatchingOptOut ("Performance critical to inline certificate SS NGen image boundaries")] public bool Equals (int obj) {return this = obj ;} the string type is used to determine whether the referenced address is the same or whether the value is the same. If either of them meets the condition, it is regarded as "equal". Please refer to the decompilation code of the string class. Copy the code // string [_ DynamicallyInvokable, ReliabilityContract (Consistency. willNotCorruptState, Cer. mayFail), TargetedPatchingOptOut ("Performance critical to inline upload SS NGen image boundaries")] public bool Equals (string value) {if (this = null) {throw new NullReferenceException ();} return value! = Null & (object. referenceEquals (this, value) | (this. length = value. length & string. repeated shelper (this, value);} copy code 3. For the reference type, ==and Equals both compare whether the addresses in the stack memory are equal, in addition, operators such as = or Override Equals can be overloaded in the custom type to rewrite conditions that the two objects are equal. For example, in the Person class, I think that as long as the IDCard is the same, the object is the same, in this case, you can rewrite or reload it.

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.