Important Interface in .NET: Work With IEqautable Interface

Welcome to the “Important Interface in C#” article series. In this series we will be talking about various useful interfaces of the .NET class library. Previous article explained the IEnumerable, ICollection, IComparable, IList, ISerializable and many more interfaces, you can read them here.

In today's article we will understand the uses of the IEquatable interface of the .NET class library. Though this interface is not widely used and its purpose also very limited. When there is a need to check the equality of two objects, we can implement the IEquitable interface in our class. As we said earlier, it's not crucial to implement an IEquitable in a class to check the equality of two objects, since there are many techniques other then that.

Anyway, let's see the position of the IEquatable interface in the .NET class library.

Namespace:  System
Assembly:   mscorlib.dll
 
The point to be noted is that there is no parameterless version of the IEqautable interface, the available version is IEquatable<T> .
 
The prototype of the IEquatable interface is as in the following.
 
public interface IEquatable
 
The interface does not contain a property and contains only one method called “Equals”.

Equals()  :  Can compare two objects
 
In this example we will implement an IEquatable<T> interface in our “Person” class and then we will implement the Equals() method to check whether or not the current object (the object that calls the method) and the argument of the Equals() method are the same. If it is the same then we will return true otherwise false. Here is the sample implementation.

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

 

namespace SelfHostingWebAPI

{

    public class Person : IEquatable<Person>

    {

        public string name { get; set; }

        public string surname { get; set; }

 

        public bool Equals(Person other)

        {

            if (other == null)

                return false;

            if (this.name == other.name && this.surname == other.surname)

            {

                return true;

            }

            else

                return false;

        }

    }

   class Program

    {

        static void Main(string[] args)

        {

 

            Person p1 = new Person { name = "sourav", surname = "kayal" };

            Person p2 = new Person { name = "sourav", surname = "kayal" };

 

            Console.WriteLine("Bith are Same:- " + p1.Equals(p2));

 

            Console.ReadLine();

        }

    }

}

The following is the output of the preceding example.

Equals Method

In the example above we compared member to member but in this example we will use the “is” operator of .NET to check the equality of the two objects. (It will check whether or not both belong to the same class. Because we are using the “is” operator, it will not check the object's property.) Have a look at the following code.
 

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Text;

 

namespace SelfHostingWebAPI

{

    public class Person : IEquatable<Person>

    {

        public string name { get; set; }

        public string surname { get; set; }

 

        public bool Equals(Person other)

        {

            return other is Person;

        }

    }

    public class dummy

    {

    }

 

   class Program

    {

        static void Main(string[] args)

        {

 

            Person p1 = new Person { name = "sourav", surname = "kayal" };

            Person p2 = new Person { name = "sourav", surname = "kayal" };

 

            Console.WriteLine("Bith are Same:- " + p1.Equals(p2));

 

            Console.WriteLine("Bith are Same:- " + p1.Equals(new dummy()));

 

            Console.ReadLine();

        }

    }

}

We are seeing that in the case of the first comparison we are getting true because the two objects belong to the same class and in the next comparison the result is false because they belong to a different class.

comparison

Conclusion

As we explained earlier, it's not crucial to implement the IEquatable<T> interface in a class to perform equality checking but it's always good to implement the IEquatable<T> interface in a class to perform such an operation. In a future article we will pick a few more interesting interfaces from the .NET class library.


Similar Articles