The difference between equals,referenceequals,==

Source: Internet
Author: User
There are several ways to compare equality in. Net

Static ReferenceEquals of Object ()

Static Equals () of object

Example of Object Equals ()

operator = =

Here's a look at their differences and usage.

ReferenceEquals

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.

int x = 10;
int y = 10;
BOOL B1 = object. ReferenceEquals (x, y);
The result here is definitely return false, but true if the reference type is compared and if two references point to the same object.

Let's just define the entity class first.

public class Person
{
private int _personid;

public int PersonId
{
get {return _personid;}
set {_personid = value;}
}

private string _FirstName;

public string FirstName
{
get {return _firstname;}
set {_FirstName = value;}
}

private string _lastname;

public string LastName
{
get {return _lastname;}
set {_lastname = value;}
}

Public person () {}

public person (int personId, string firstName, String lastName)
{
This._personid = personId;
This._firstname = FirstName;
This._lastname = LastName;
}
}
Calling code

Person Person1 = new person (1, "Edrick", "Liu");
Person Person2 = new Person (2, "Meci", "Luo");
Person Person3 = Person2;
BOOL Br1= object. ReferenceEquals (Person1,person2);
BOOL Br2 = object. ReferenceEquals (Person2, Person3);
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.

Instance 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?

MSDN gives us a list of some guidelines

X.equals (x) returns true in addition to cases involving floating-point types.

X.equals (y) returns the same value as Y.equals (x).

If both x and Y are NaN, x.equals (y) returns True.

When and only if X.equals (z) returns True, (X.equals (y) && y.equals (z)) returns True.

Successive calls to X.equals (y) return the same value as long as the objects referenced by x and Y are not modified.

X.equals (Nullnothingnullptrnull reference (Nothing in Visual Basic) returns false.

Let's take a look at the rewrite code

public class Person
{
private int _personid;

public int PersonId
{
get {return _personid;}
set {_personid = value;}
}

private string _FirstName;

public string FirstName
{
get {return _firstname;}
set {_FirstName = value;}
}

private string _lastname;

public string LastName
{
get {return _lastname;}
set {_lastname = value;}
}

Public person () {}

public person (int personId, string firstName, String lastName)
{
This._personid = personId;
This._firstname = FirstName;
This._lastname = LastName;
}

public override bool Equals (object obj)
{
if (obj! = null && obj is person)
{
Person p = obj as person;

return (PersonId = = P.personid) && (FirstName = = p.firstname) && (LastName = p.lastname);
}
Else
{
return false;
}
}

public override int GetHashCode ()
{
Return base. GetHashCode () ^personid;
}
}
Calling code

Person Person1 = new person (1, "Edrick", "Liu");
Person Person2 = new Person (2, "Meci", "Luo");
Person Person3 = Person2;
Person person4 = new person (1, "Edrick", "Liu");

Console.WriteLine (Person4. Equals (Person1));
Console.WriteLine (Person4. Equals (Person2));
We can see the result, the first one is true, the second is false. An exception cannot occur when overloading. So if there is a class that inherits the person, we change how to compare the derived class.

public class Student:person
{
private int _studentnumber;

public int Studentnumber
{
get {return _studentnumber;}
set {_studentnumber = value;}
}

Public Student () {}

Public Student (int personId, string firstName, string lastName, int studentnumber)
{
This. PersonId = PersonId;
This. FirstName = FirstName;
This. LastName = LastName;
This._studentnumber = Studentnumber;
}

public override bool Equals (object obj)
{
if (obj! = null && obj is person)
{
Student s = obj as Student;
Return base. Equals (obj) &&StudentNumber==s.StudentNumber;
}
Else
{
return false;
}
}

public override int GetHashCode ()
{
Return base. GetHashCode () ^studentnumber;
}
}
Calling code

Student S1 = new Student (1, "Edrick", "Liu", 1);
Student s2 = new Student (2, "Meci", "Luo", 2);
Student s3 = new Student (1, "Edrick", "Liu", 1);

Console.WriteLine (S1. Equals (S2));
Console.WriteLine (S1. Equals (S3));
We only need to call the parent class's Equals method and compare the new values in the derived class.

Static equals

This method is a more interesting method. This method is also static, it can compare references, can compare value types. If the type of comparison overloads the equals of the instance, it can also compare the value of the object. So it returns true there are 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

Student S1 = new Student (1, "Edrick", "Liu", 1);
Student s2 = new Student (2, "Meci", "Luo", 2);
Student s3 = new Student (1, "Edrick", "Liu", 1);
Student S4 = S3;

Console.WriteLine (object. Equals (S1,S3));
Console.WriteLine (object. Equals (S4, S3));
Both are true, where static equals has a difference from static equalsreference, and static equals throws an exception if one of the arguments is null.

An interesting phenomenon is discussed below, what happens if you overload the equals but do not overload the = = operator

Student S1 = new Student (1, "Edrick", "Liu", 1);
Student s2 = new Student (2, "Meci", "Luo", 2);
Student s3 = new Student (1, "Edrick", "Liu", 1);
Student S4 = S3;

Console.WriteLine (S1==S3);
Console.WriteLine (S3==S4);
The first one is false and the second is true. This obviously does not conform to our intent, so overloading equals must overload = =, as well as overloading = = must also overload equals. This is in line with our intentions and ensures that the code works according to our intentions when using the collection. Because the collection coll[0]==co[0] actually compares the reference, but if our equals compares the value of the object then the last code will not run as I expected.

= = 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.




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.