What's the difference between IComparable & IEquatable interfaces?

C#InterfaceIcomparableIequatable

C# Problem Overview


Both the interfaces seem to compare objects for equality, so what are the major differences between them?

C# Solutions


Solution 1 - C#

IEquatable tests whether two objects are equal.

IComparable imposes a total ordering on the objects being compared.

For example, IEquatable would tell you that 5 is not equal to 7. IComparable would tell you that 5 comes before 7.

Solution 2 - C#

IEquatable<T> for equality.

IComparable<T> for ordering.

Solution 3 - C#

In addition to Greg D's answer:

You might implement IComparable without implementing IEquatable for a class where a partial ordering makes sense, and where you very definitely want the consumer to infer that just because CompareTo() returns zero, this does not imply that the objects are equal (for anything other than sorting purposes).

Solution 4 - C#

As stated on the MSDN Page for IEquatable:

> The IComparable interface defines > the CompareTo method, which determines > the sort order of instances of the > implementing type. The IEquatable > interface defines the Equals method, > which determines the equality of > instances of the implementing type.

Equals vs. CompareTo

Solution 5 - C#

IComparable <T> defines a type specific comparison method which can be used to order or sort objects.

IEquatable <T> defines a generalized method which can be used to implement for determining equality.


Let's say you have Person class

public class Person
{
	public string Name { get; set; }
	public int Age { get; set; }
}

Person p1 = new Person() { Name = "Person 1", Age = 34 };
Person p2 = new Person() { Name = "Person 2", Age = 31 };
Person p3 = new Person() { Name = "Person 3", Age = 33 };
Person p4 = new Person() { Name = "Person 4", Age = 26 };

List<Person> people = new List<Person> { p1, p2, p3, p4 };

To sort these objects you can use people.Sort();.

But this will thrown an an exception.

enter image description here

Framework doesn't know how to sort these objects. You need to tell how to sort implementing IComparable interface.

public class Person : IComparable
{
	public string Name { get; set; }
	public int Age { get; set; }

	public int CompareTo(object obj)
	{
		Person otherPerson = obj as Person;
		if (otherPerson == null)
		{
			throw new ArgumentNullException();
		}
		else
		{
			return Age.CompareTo(otherPerson.Age);
		}
	}
}

This will sort the array properly with Sort() method.


Next to compare two objects you can use Equals() method.

var newPerson = new Person() { Name = "Person 1", Age = 34 };
var newPersonIsPerson1 = newPerson.Equals(p1);

This will return false because Equals method doesn't know how to compare two objects. Therefore you need to implement IEquatable interface and tell the framework how to do the comparison. Extending on the previous example it'll look like this.

public class Person : IComparable, IEquatable<Person>
{
	//Some code hidden

	public bool Equals(Person other)
	{
		if (Age == other.Age && Name == other.Name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoftwareGeekView Question on Stackoverflow
Solution 1 - C#Greg DView Answer on Stackoverflow
Solution 2 - C#Islam YahiateneView Answer on Stackoverflow
Solution 3 - C#Damien_The_UnbelieverView Answer on Stackoverflow
Solution 4 - C#Will EddinsView Answer on Stackoverflow
Solution 5 - C#NipunaView Answer on Stackoverflow