equals方法实现助手(C#) [英] Equals method implementation helpers (C#)

查看:138
本文介绍了equals方法实现助手(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我写了一些数据类,我通常会花那么多时间写IEquatable实施。

Everytime I write some data class, I usually spend so much time writing the IEquatable implementation.

我写的最后一堂课是这样的:

The last class I wrote was something like:

public class Polygon
{
    public Point[] Vertices { get; set; }
}

实施IEquatable是exaustive。当然,C#3.0 / LINQ有很大帮助,但是顶点可以转移和/或以相反的顺序,并增添了许多复杂的equals方法。经过多次的单元测试,以及相应的实施,我放弃了,并改变了我的应用程序只接受三角形,这IEquatable实现只需要11单元测试来全面覆盖。

Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the reverse order, and that adds a lot of complexity to the Equals method. After many unit tests, and corresponding implementation, I gave up, and changed my application to accept only triangles, which IEquatable implementation required only 11 unit tests to be fully covered.

有任何工具或技术,可以帮助实现Equals和GetHash code?

There is any tool or technique that helps implementing Equals and GetHashCode?

推荐答案

我用ReSharper的生成平等成员。它可以随意实施 IEquatable< T> 还有,如果你想要的(当然,你不会这么做,但是这是很酷无论如何)覆盖运营商

I use ReSharper to generate equality members. It will optionally implement IEquatable<T> as well as overriding operators if you want that (which of course you never do, but it's cool anyway).

等于的实现包括的Object.Equals的倍率(对象),以及一个强类型的变体(可避免不必要的类型检查)。较小的类型版本要求进行类型检查后,强类型之一。强类型版本进行引用相等检查( Object.ReferenceEquals(对象,对象)),然后比较所有字段的值(当然,只有那些你告诉发电机包括)​​。

The implementation of Equals includes an override of Object.Equals(Object), as well as a strongly typed variant (which can avoid unnecessary type checking). The lesser typed version calls the strongly typed one after performing a type check. The strongly typed version performs a reference equality check (Object.ReferenceEquals(Object,Object)) and then compares the values of all fields (well, only those that you tell the generator to include).

至于 GetHash code ,领域的 GetHash code 值一个聪明的因式分解是结合(使用选中来避免溢出异常,如果你使用编译器的检查选项)。各字段的值(除了第一个)的由素数被组合之前相乘。您还可以指定哪些字段将永远不会为空,它会删除任何null检查。

As for GetHashCode, a smart factorisation of the field's GetHashCode values are combined (using unchecked to avoid overflow exceptions if you use the compiler's checked option). Each of the field's values (apart from the first one) are multiplied by prime numbers before being combined. You can also specify which fields would never be null, and it'll drop any null checks.

下面就是你通过pressing得到您的多边形 ALT + INSERT 然后选择生成平等成员

Here's what you get for your Polygon class by pressing ALT+Insert then selecting "Generate Equality Members":

public class Polygon : IEquatable<Polygon>
{
    public Point[] Vertices { get; set; }

    public bool Equals(Polygon other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(other.Vertices, Vertices);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (Polygon)) return false;
        return Equals((Polygon) obj);
    }

    public override int GetHashCode()
    {
        return (Vertices != null ? Vertices.GetHashCode() : 0);
    }
}

一些我上面所讲的功能并不适用,因为这里只有一个字段。还要注意,它没有检查数组的内容。

Some of the features I talked about above don't apply as there is only one field. Note too that it hasn't checked the contents of the array.

在普通不过,ReSharper的泵出在几秒钟只是一个问题很多优秀的code。这功能是pretty的低我要做的事情中,使ReSharper的这样一个惊人的工具。

In general though, ReSharper pumps out a lot of excellent code in just a matter of seconds. And that feature is pretty low on my list of things that makes ReSharper such an amazing tool.

这篇关于equals方法实现助手(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆