Using The IEquatable Interface In C#

Introduction

 
One of the most used functionalities in our C# classes is the ability to compare two instances of a class. This is done using the Equals keyword. This will compare to see if the reference to both the classes is the same. However, we might want to compare them and see if they are equal, based on some other field. Today, we will see how this is done in our C# classes.
 

Creating the Class

 
We will create a .NET Core console application using Visual Studio 2019 Community edition, as shown below:
 
Using The IEquatable Interface In C#
 
Using The IEquatable Interface In C#
 
Next, we create a Student Class inside the “Program.cs” file, as shown below:
  1. public class Student : IEquatable<Student>  
  2.     {  
  3.         public int ID { get; set; }  
  4.         public string Name { get; set; }  
  5.         public string Program { get; set; }  
  6.         public int Year { get; set; }  
  7.         public float GPA { get; set; }  
  8.   
  9.         public bool Equals([AllowNull] Student otherStudent)  
  10.         {  
  11.             return (this.ID == otherStudent.ID);  
  12.         }  
  13.     }  
Let's look at the code in detail. To make the class equitable to another instance of the same class, we use the IEquatable<T> interface and code the Equals method. This method must return a true on the condition we want satisfied to consider the classes equal or else we need to return false. In the above example, we compare the ID field of the two classes and if they are the same, we consider the classes equal.
 
To test this out, we write the following code in the Main function, as shown below:
  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.           var studentA = new Student() { ID = 1, Name = "John Doe", Program = "BCS", Year = 2020, GPA = 2.75F };  
  6.           var studentB = new Student() { ID = 2, Name = "Jane Doe", Program = "BCS", Year = 2020, GPA = 3.4F };  
  7.           var studentC = new Student() { ID = 1, Name = "John Jane", Program = "BCS", Year = 2019, GPA = 2.71F };  
  8.   
  9.           var AtoB = studentA.Equals(studentB);  
  10.           var AtoC = studentA.Equals(studentC);  
  11.   
  12.           Console.WriteLine($"Student A is equal to Student B = {AtoB}");  
  13.           Console.WriteLine($"Student A is equal to Student C = {AtoC}");  
  14.   
  15.           Console.ReadKey();  
  16.       }  
  17.   }  
Below is the result:
 
Using The IEquatable Interface In C#
 
Here, we see that when we compare StudentA to Student, we get false as the IDs for both are different. When we compare StudentA to StudentC, we get True as the Ids for these two classes match.
 

Summary

 
In this article, we looked at how we can implement the IEquatable<T> interface and test if a class instance is equal to another instance of the same class. This can be based on a single field as we did in this example or on multiple fields as required.