开发者

C# -IComparable<T> and IEquatable<T> [duplicate]

This question already has answers here: Closed 9 years ago.

Possible Duplicate:

Wh开发者_C百科at’s the difference between IComparable & IEquatable interfaces?

What is the major difference between IComparable<T> and IEquatable<T> ? When to use each one specifically?


The IComparable defines order (less than, greater than). The method defined by it is CompareTo with which you can determine the order between two elements.

IEquatable defines equality. The method defined by it is Equals, it lets you determine if two elements are equal.

Compare example, ordering Person by age:

public class Person : IComparable<Person>
{
    public int Age { get; set; }
    public int ID { get; set; }

    public int CompareTo(Person other)
    {

        return Math.Sign(Age - other.Age); // -1 other greater than this
                                           // 0 if same age
                                           // 1 if this greater than other
    }
}

Sort usage:

[Test]
public void SortTest()
{
    var persons = new List<Person>
                      {
                          new Person { Age = 0 },
                          new Person { Age = 2 },
                          new Person { Age = 1 }
                      };

    persons.Sort();

    Assert.AreEqual(0, persons[0].Age);
    Assert.AreEqual(1, persons[1].Age);
    Assert.AreEqual(2, persons[2].Age);
}

Equality example for identifying Person by ID:

public class Person : IEquatable<Person>
{
    public int Age { get; set; }
    public int ID { get; set; }

    public bool Equals(Person other)
    {
        return ID == other.ID;
    }
}


If you're sorting on an IEnumerable<T>, use IComparable<T>, if comparing two instances for equality; IEquatable<T>


Generally anything that sorts will use the IComparable interface of your classes if available otherwise .Net will apply default ordering which may be entirely unexpected.
If you don't define one the default will be used - in the case where you inherit from something that is IComparable then if some base class implements this then the sort order will be whatever the base class defines.

IEquatable defines equality and lets you define how .Net determines whether instances of your classes are equal with the Equals method. If you don't define one then equality is based on reference equality by default. This means that unless you are comparing two references to the same instance of a class then they won't be equal, even when you would expect them to be equal. Again if you inherit from a base class that does implement the interface and you don't implement it yourself in your class you'll be using the base classes equality calculation.

Generally it's important to implement these if you are going to use common .Net functions in conjunction with them. For example if you use the Contains() function on a collection of your class then it will try and use the IEquatable interface

From MSDN:

The IEquatable<(Of <(T>)>) interface is used by generic collection objects such as Dictionary<(Of <(TKey, TValue>)>), List<(Of <(T>)>), and LinkedList<(Of <(T>)>) when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.

The IComparable<(Of <(T>)>) interface provides a strongly typed comparison method for ordering members of a generic collection object. Because of this, it is usually not called directly from developer code. Instead, it is called automatically by methods such as List<(Of <(T>)>)..::.Sort()()() and Add

.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜