简体   繁体   English

实施 IEqualityComparer

[英]Implementing IEqualityComparer

I've added to my class the IEqualityComparer implementation, not sure if the code bellow is the correct one, especially the Equals(object x, object y) functions: should we override or make a new implementation of the Equals method, like this: ?我已经在我的 class 中添加了IEqualityComparer实现,不确定下面的代码是否正确,尤其是Equals(object x, object y)函数:我们应该overridenew实现 Equals 方法,如下所示: ?

public class PropertySettings : IEqualityComparer
{
    public int? Precision { get; set; }
    public double? Min { get; set; }
    public double? Max { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is PropertySettings))
            return false;

        var ps = obj as PropertySettings;

        return
            ps.Precision == this.Precision &&
            ps.Min == this.Min &&
            ps.Max == this.Max;
    }

    public bool Equals(object x, object y)
    {
        if (x != null && x is PropertySettings)
            return x.Equals(y);
        else
            return object.Equals(x, y);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(Precision, Min, Max);
    }

    public int GetHashCode(object obj)
    {
        if (obj == null)
            return 0;
        if (obj is PropertySettings)
            return (obj as PropertySettings).GetHashCode();
        else
            return obj.GetHashCode();
    }
}

在此处输入图像描述

According to the IEqualityComparer code example provided by microsoft, you will want to use the new keyword (so hiding the Equals implementation of the object) for implementing Equals.根据 Microsoft 提供的IEqualityComparer代码示例,您需要使用 new 关键字(因此隐藏对象的 Equals 实现)来实现 Equals。

https://learn.microsoft.com/en-us/do.net/api/system.collections.iequalitycomparer.equals?view.net-6.0#system-collections-iequalitycomparer-equals(system-object-system-object) https://learn.microsoft.com/en-us/do.net/api/system.collections.iequalitycomparer.equals?view.net-6.0#system-collections-iequalitycomparer-equals(系统对象系统对象)

As pointed out by Jeroen Mostert below, these can be incorrect.正如下面Jeroen Mostert所指出的,这些可能是不正确的。 His recommendation of implementing IEqualityComparer.Equals works as well.他关于实施IEqualityComparer.Equals的建议也很有效。 You can also use override .您也可以使用override These will all provide different functionality based on what you cast to.这些都将根据您投射到的内容提供不同的功能。 Here is a brief explanation:下面是一个简短的解释:

https://learn.microsoft.com/en-us/do.net/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords https://learn.microsoft.com/en-us/do.net/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords

Basically, using override would mean that you will use your.Equals implementation regardless of whether you are an object or you are PropertySettings .基本上,使用override意味着您将使用 your.Equals 实现,无论您是object还是PropertySettings If you use new when you are an object you will use the base .Equals(... functionality (that of object in this case). If you explicitly implement IEqualityComparer.Equals(... then you will use the .Equals when you cast your object as an IEqualityComparer . This leaves you with the choice of how you are using your class.如果您在 object 时使用new ,您将使用基本的.Equals(...功能(在本例中为object的功能)。如果您显式实现IEqualityComparer.Equals(...那么您将使用.Equals当您将您的 object 转换为IEqualityComparer 。这让您可以选择如何使用 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM