How do I check if an object is equal to a new object of the same class?
There are a couple of ways you can do this. By default Equals()
and ==
check for reference equality, meaning:
Person a = new Person();
Person b = a:
a.Equals(b); //true
a == b; //true
And therefore, the objects are not compared for value equality, meaning:
Person a = new Person { id = 1, name = "person1" };
Person b = new Person { id = 1, name = "person1" };
a.Equals(b); //false
a == b; //false
To compare objects for their values you can override the Equals()
and GetHashcode()
methods, like this:
public override bool Equals(System.Object obj)
{
if (obj == null)
return false;
Person p = obj as Person;
if ((System.Object)p == null)
return false;
return (id == p.id) && (name == p.name);
}
public bool Equals(Person p)
{
if ((object)p == null)
return false;
return (id == p.id) && (name == p.name);
}
public override int GetHashCode()
{
return id.GetHashCode() ^ name.GetHashCode();
}
Now you will see other results when comparing:
Person a = new Person { id = 1, name = "person1" };
Person b = new Person { id = 1, name = "person1" };
Person c = a;
a == b; //false
a == c; //true
a.Equals(b); //true
a.Equals(c); //true
The ==
operator is not overridden and therefore still does reference comparison. This can be solved by overloading it as well as the !=
operator:
public static bool operator ==(Person a, Person b)
{
if (System.Object.ReferenceEquals(a, b))
return true;
if ((object)a == null || (object)b == null)
return false;
return a.id == b.id && a.name == b.name;
}
public static bool operator !=(Person a, Person b)
{
return !(a == b);
}
Now running the checks results in following:
Person a = new Person { id = 1, name = "person1" };
Person b = new Person { id = 1, name = "person1" };
Person c = a;
a == b; //true
a == c; //true
a.Equals(b); //true
a.Equals(c); //true
More reading:
- Guidelines for Implementing Equals and the Equality Operator (==)
This all depends on what you are trying to compare, by default Equals
will compare by reference therefore
a == b
in your example will always be false
. However, if you did something like
Person a = new Person();
Person b = a;
Then a == b
would be true
because both a
and b
are using the same reference.
do i have to override the Object.Equals() method?
Overriding Equals and GetHashCode is the recommended approach, however, (for arguments sake) it's not the only way. You could, for example, override the ==
operator and do your comparison in there. However, there are limitations with going down that route alone.
Most comparison checks, if not all, will use Equals
internally which is why it's the preferred approach. See Guidelines for Implementing Equals and the Equality Operator (==).