Objective
It's important to understand the concept of Equals() in C# to get the desired output. When I started programming, sometimes when I tried to check whether or not two objects are equal, surprisingly I used to get the wrong output. Then I spent a little time on the concept and finally got to understand why I had reviously gotten the wrong output.
Let's say we have the following two strings:
- string first = "Arun";
- string second = "Arun";
To check the equality we can do either of these:
- Console.WriteLine(first == second);
- Console.WriteLine(first.Equals(second));
If we run both of the cases then we will get output as “true”.
This process is true for all data types like int, double and so on. Now you might ask, where is the problem?
The ProblemThe real problem occurs when we do the same operation to check whether two “custom” objects are equal or not. Let's elaborate this with an example. Say, we have an Employee class as follows:
- public class Employee
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public double Salary { get; set; }
-
- Employee(string name, int age, double sal)
- {
- Name = name;
- Age = age;
- Salary = sal;
- }
-
- public List<Employee> GetEmployees()
- {
- var employeeList = new List<Employee>();
-
- employeeList.Add(new Employee("Arunava", 29, 25000));
- employeeList.Add(new Employee("Aamir", 56, 50000));
- employeeList.Add(new Employee("Santanu", 78, 39000));
- employeeList.Add(new Employee("Bubu", 67, 40000));
-
- return employeeList;
- }
- }
A simple class with 3 properties, a constructor and one method to get a list of employees. Now let's check our Main method:
- static void Main(string[] args)
- {
- var employeeList = Employee.GetEmployees();
-
- var checkEmployee = new Employee("Arunava", 29, 25000);
- Console.WriteLine(employeeList.Contains(checkEmployee));
- Console.ReadLine();
-
- }
What are we expecting here as output?
The
“checkEmployee” is definitely present there in
“employeeList”, but we get the output as:
Surprising. Isn't it?
Solution
Nothing is wrong with your assumption. How does the computer (in other words, the complier) know that you meant to determine whether the objects are equal with the Name, Age and Salary are same? What if, somebody says, the objects are equal if their names are the same? Another guy might say, no, I will accept that the objects are equal if the Name and Age are the same. So, C# left it to the user to define the equality condition. And guess what, you can use the Equals() method to define the condition.
Implementing Equals()There are guidelines to see how to implement the Equals method in MSDN:
https://msdn.microsoft.com/en-us/library/ms173147.aspxGenerally, whenever I create a custom class and I know that I need to do an Equality check I implement Equals() and also keep GethashCode() and operator == in sync as well. This is how I implemented my Equality check for the class of this example:
- public override bool Equals(object second)
- {
- return Equals(second as Employee);
- }
-
- public virtual bool Equals(Employee second)
- {
- if (second == null) { return false; }
- if (object.ReferenceEquals(this, second)) { return true; }
- return ((this.Name == second.Name) && (this.Salary == second.Salary) && (this.Age == second.Age));
- }
-
- public override int GetHashCode()
- {
- return this.Age;
- }
-
- public static bool operator ==(Employee item1, Employee item2)
- {
- if (object.ReferenceEquals(item1, item2)) { return true; }
- if ((object)item1 == null || (object)item2 == null) { return false; }
- return ((item1.Name == item2.Name) && (item1.Salary == item2.Salary) && (item1.Age == item2.Age));
- }
-
- public static bool operator !=(Employee item1, Employee item2)
- {
- return ((item1.Name == item2.Name) && (item1.Salary == item2.Salary) && (item1.Age == item2.Age));
- }
ConclusionAfter implementing this, if we run the same code we get our desired output:
- var employeeList = Employee.GetEmployees();
-
- var checkEmployee = new Employee("Arunava", 29, 25000);
- Console.WriteLine(employeeList.Contains(checkEmployee));
- Console.ReadLine();
Output
So remember, whenever you have a condition to check the Equality between objects, don't forget to correctly implement Equals() and HashCodes(). Why did the Sting at the beginning provide the correct result earlier? Well, those classes have already implemented Equals() and HashCode().
Enjoy coding.