IComparable 和 IComparable 之间有什么区别? IEquatable接口?

发布于 2024-08-24 15:26:17 字数 39 浏览 10 评论 0原文

这两个接口似乎都比较对象是否相等,那么它们之间的主要区别是什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空袭的梦i 2024-08-31 15:26:17

IEquatable 测试两个对象是否相等。

IComparable 对正在比较的对象强加总排序。

例如,IEquatable 会告诉您 5 不等于 7。IComparable 会告诉您 5 在 7 之前。

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.

北斗星光 2024-08-31 15:26:17

IEquatable 表示相等。

IComparable 用于排序。

IEquatable<T> for equality.

IComparable<T> for ordering.

被你宠の有点坏 2024-08-31 15:26:17

除了 Greg D 的回答之外:

您可以实现 IComparable 而不实现 IEquatable 对于一个类,其中部分排序有意义,并且您绝对希望消费者推断出这一点因为 CompareTo() 返回零,这并不意味着对象是相等的(除了排序目的之外)。

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).

烟─花易冷 2024-08-31 15:26:17

正如 IEquatable 的 MSDN 页面 中所述:

IComparable 接口定义
CompareTo 方法,它确定
实例的排序顺序
实施类型。 IE等值表
接口定义了Equals方法,
这决定了平等
实现类型的实例。

等于CompareTo

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

中二柚 2024-08-31 15:26:17

IComparable 定义了一种特定于类型的比较方法,可用于对对象进行排序或排序。

IEquatable 定义了可用于实现确定相等性的通用方法。


假设您有 Person 类,

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 };

要对这些对象进行排序,您可以使用 people.Sort();

但这会抛出异常。

输入图像描述这里

框架不知道如何对这些对象进行排序。您需要告诉如何排序实现 IComparable 接口。

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);
        }
    }
}

这将使用 Sort() 方法对数组进行正确排序。


接下来要比较两个对象,您可以使用 Equals() 方法。

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

这将返回false,因为Equals方法不知道如何比较两个对象。因此,您需要实现 IEquatable 接口并告诉框架如何进行比较。扩展前面的示例,它看起来像这样。

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;
        }
    }
}

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文