= = Equals ReferenceEquals comparison

Source: Internet
Author: User

= = is an operator

ReferenceEquals and equals as functions

=========================================================

ReferenceEquals is used to compare references of reference types to whether they point to the same object. It can only compare reference types. The value type is always returned false when it is passed to it, because the value type is first boxed when it is a parameter, the boxed value type even refers to equal, but also two different objects, so the variable is pointing to different objects, so it always returns false.

For example:

1 int Ten  2int3boolObject

The result must be a return of false, but true if the reference type is compared and if two references point to the same object.

Again for example:

1   Public class Person2     {3          Public stringPersonId {Get;Set; }4          Public stringFirstName {Get;Set; }5          Public stringLastName {Get;Set; }6     }7     class Program8     {9 Ten         Static voidMain (string[] args) One         { APerson Person1 =Newperson {PersonId ="1", FirstName ="Qu", LastName ="Geng"}; -Person Person2 =Newperson {PersonId ="2", FirstName ="QU2", LastName ="Geng2" }; -Person Person3 =Person2; the             BOOLBR1 =Object. ReferenceEquals (Person1, person2); -             BOOLBR2 =Object. ReferenceEquals (Person2, Person3); - Console.WriteLine (BR1); - Console.WriteLine (BR2); + Console.readkey (); -         } +}

We can find that the first one returns false and the second returns true. So what if one of the objects is null, or two objects are null? The result will be false, if two are null? The result is true. They do not throw an exception.

Equals

Example equals is a more complex comparison. Instance equals can compare references to the same object, and can compare objects by value. If we were to compare objects by value, we would need to overload the equals object to implement our comparison logic. Equals is also supported by default for comparing value types. So how do we overload equals to make the object compare with the value equality?

It returns true in three cases:

1, reference to the same object

2, Comparison of two null

3, an instance of equals is overloaded with a method that returns true

= = operator

The = = operator is not quite the same as the equals of the instance, = = is the operator, and equals is the method. They can all be rewritten. Reference and comparison values can be compared by default. Overloading of = = can refer to operator overloading in the operator article.

To summarize their differences:

ReferenceEquals: Static methods, cannot be overridden, can only compare references, if one parameter is NULL, returns FALSE, does not throw an exception, and always returns False if the value type is compared.

Equals: Instance methods, which can be overridden by default to compare references or to compare values. You can compare objects by value.

Static equals: Static method, cannot be overridden. If you do not override equals, compare references, or compare values. If the Equals method is overloaded. Compares a reference, or compares a value, or compares it by the overridden equals, throws an exception if one of the arguments is null

= = Operator: You can compare by reference or by value. can be overridden. is the operator operator.

Finally, if equals is overloaded, it is best to overload the GetHashCode, which must be overloaded with the = = operator.

= = Equals ReferenceEquals comparison

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.