C# has an "Equals" method which can be used to compare two objects. I will try to explain the Equality and Identity concept with examples here.
Example 1
- namespace TestEqualityDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- TestEquality test1 = new TestEquality();
- test1.FirstName = "Tom";
- test1.LastName = "Cruise";
-
- TestEquality test2 = new TestEquality();
- test2.FirstName = "Tom";
- test2.LastName = "Cruise";
- TestEquality test3 = test2;
- bool areEqual = test1.Equals(test2);
- System.Console.WriteLine("Are test1 and test2 are Equal:" + areEqual);
-
- areEqual = test2.Equals(test3);
- System.Console.WriteLine("Are test2 and test3 are Equal:" + areEqual);
- System.Console.ReadLine();
- }
- }
-
- class TestEquality
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
- }
Output
- Are test1 and test2 Equal:False
- Are test2 and test3 Equal:True
In the above example,
- Even if test1 and test2 contain the same value for FirstName and LastName, the "Equals" method returns False. That is because default implementation of Equals method does not check for Equality; it checks for Identity. This means test1 and test2 must refer to the exact same object, then only it will return True, otherwise, it will return False.
- As test2 and test3 are referring to the same object, it returns True. As per the above program we can conclude that default implementation of Equals checks for Identity which means it will return True only if two variables are referring to the same object.
Then, the question arises how to check for equality in C# ,and the answer for this is to override the default implementation of Equals method.
Here is the default implementation of Equals method.
- public virtual bool Equals(Object obj)
- {
-
- if (this == obj)
- {
- return true;
- }
- return false;
- }
The following points must be considered to override Equals method,
- If the obj argument is null it returns false
- If this and obj refers to the same object it returns True. This can improve the performance when comparing values with many fields
- If this and obj are referring to different type it returns False, because there is no point in comparing the objects of different type; for example, if we compare a String object with DateTime object because they will not be equal in any situation.
- Compare each field of this object with the respective field of obj object.
- Finally, call the base class Equals method.
The above are the best steps to override Equals method for comparison.
Let's implement Equals method for TestEquality class.- namespace TestEqualityDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- TestEquality test1 = new TestEquality();
- test1.FirstName = "Tom";
- test1.LastName = "Cruise";
-
- TestEquality test2 = new TestEquality();
- test2.FirstName = "Tom";
- test2.LastName = "Cruise";
-
- TestEquality test4 = new TestEquality();
- test2.FirstName = "Will";
- test2.LastName = "Smith";
-
- TestEquality test3 = test2;
- bool areEqual = test1.Equals(test2);
- System.Console.WriteLine("Are test1 and test2 Equal:" + areEqual);
-
- areEqual = test2.Equals(test3);
- System.Console.WriteLine("Are test2 and test3 Equal:" + areEqual);
- System.Console.ReadLine();
-
- areEqual = test2.Equals(test4);
- System.Console.WriteLine("Are test2 and test4 Equal:" + areEqual);
- System.Console.ReadLine();
- }
- }
-
- class TestEquality
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
-
- public override bool Equals(System.Object obj)
- {
-
- if (obj == null)
- return false;
-
- if (this == obj)
- return true;
-
-
- if (this.GetType() != obj.GetType())
- return false;
-
-
- TestEquality test = (TestEquality)obj;
- if (this.FirstName == test.FirstName &&
- this.LastName == test.LastName)
- {
- return true;
- }
- return false;
- }
- }
- }
Output
- Are test1 and test2 Equal:True
- Are test2 and test3 Equal:True
- Are test2 and test4 Equal:False
In the above program,
- All the fields of test1 and test2 are equal so Equals method returns true.
- test2 and test3 refers to the same object so it will also return true
- In test2 and test4 if values are different for FirstName and LastName it returns false
The following rules must be followed by overridden Equals method,
- Equals must be reflexive, that is x.Equals(x) must return true.
- Equals must be symmetric, that is x.Equals(y) must return the same value as y.Equals(x)
- Equals must be transitive i.e if x.Equals(y) returns true and y.Equals(z) returns true then x.Equals(z) must return true.
If overridden Equals method does not follow the above rules, there are chances that your application will break or give unexpected results.