对于引用类型,如何使用 IEquatable来实现? 减少铸件的使用?

发布于 2024-07-21 07:15:28 字数 89 浏览 7 评论 0原文

我读过几篇文章,

使用 IEquatable 的引用类型减少了强制转换的使用

有人可以提供一个令人信服的示例吗?

I've read in several articles that

for reference types using IEquatable reduces the use of casting

Can someone kindly provide a convincing example.

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

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

发布评论

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

评论(3

倾`听者〃 2024-07-28 07:15:28

只是在 Dario 的解释

class Person : IEquatable<Person>
{
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        if (obj is Person)
        {
            Person other = (Person)obj;
            // check equality
        }
        return base.Equals(obj);
    }

    #region IEquatable<Person> Members

    public bool Equals(Person other)
    {
        // check equality without cast
    }

    #endregion
}

注意:这只是一个小片段,说明了为什么这可以避免 cast。 要正确实施,请检查文档

如果你实现 IEquatable<(Of
<(T>)>),您还应该覆盖
的基类实现
对象 ..::.Equals(Object) 和
GetHashCode 以便他们的行为是
与 的 一致
IEquatable<(Of <(T>)>)..::.等于
方法

Just to add a simple example after Dario's explanation:

class Person : IEquatable<Person>
{
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        if (obj is Person)
        {
            Person other = (Person)obj;
            // check equality
        }
        return base.Equals(obj);
    }

    #region IEquatable<Person> Members

    public bool Equals(Person other)
    {
        // check equality without cast
    }

    #endregion
}

Note: This is just a little fragment that shows why this avoids a cast. For a correct implementation check the docs:

If you implement IEquatable<(Of
<(T>)>), you should also override the
base class implementations of
Object..::.Equals(Object) and
GetHashCode so that their behavior is
consistent with that of the
IEquatable<(Of <(T>)>)..::.Equals
method

唯憾梦倾城 2024-07-28 07:15:28

您知道这篇文章吗?
它说

如果您想拥有自己的实现,可以重写此方法。 由于 Equals 方法具有 Object 类型的参数,因此需要进行强制转换才能访问类特定成员。

这就是 IEquatable 接口的用武之地。IEquatable 是 .NET 2.0 中的一个新通用接口,它允许您执行与 System.Object.Equals 方法相同的操作,但无需执行强制转换。 因此,在实现这个接口时,可以减少强制转换的次数,这对于性能来说是一件好事。 特别是在大量使用泛型集合时,因为泛型集合在其某些方法(List.Equals()、List.IndexOf()、List.LastIndexOf() 等)中使用了这种相等比较。

由于其泛型类型参数,IEquatable 可以在编译时提供类型检查,因此您不必与 object 进行强制转换和后期绑定。

Do you know this article?
It says

If you want to have your own implementation, you can override this method. Since the Equals method has a parameter of type Object, a cast will be needed in order to be able to access class specific members.

This is where the IEquatable interface comes in. The IEquatable is a new generic interface in .NET 2.0 that allows you to do the same as the System.Object.Equals method but without having to perform casts. So, when implementing this interface, you can reduce the number of casts, which is a good thing for performance. Especially when working a lot with generic collections, since generic collections make use of this equality comparison in some of their methods (List.Equals(), List.IndexOf(), List.LastIndexOf(), ...).

Due to its generic type parameter, IEquatable<T> can provide type-checking at compiletime so you don't have to cast and late-bind with objects.

谈场末日恋爱 2024-07-28 07:15:28

我相信您的答案就在这里, http://msdn.microsoft.com/en -us/library/ms131190.aspx。 阅读页面上的备注部分。

I believe your answer is here, http://msdn.microsoft.com/en-us/library/ms131190.aspx. Read the remarks section on the page.

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