对于引用类型,如何使用IEquatable 减少使用转换?

时间:2022-03-19 13:33:57

I've read in several articles that

我读过几篇文章

for reference types using IEquatable reduces the use of casting

使用IEquatable的参考类型减少了使用铸造

Can someone kindly provide a convincing example.

有人可以提供一个令人信服的例子。

3 个解决方案

#1


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

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

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

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

#2


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.

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

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

这就是IEquatable接口的用武之地.IEququatable是.NET 2.0中的一个新的通用接口,它允许你像System.Object.Equals方法一样做,但不必执行强制转换。因此,在实现此接口时,您可以减少强制转换的数量,这对性能来说是一件好事。特别是在使用泛型集合时,由于泛型集合在其某些方法(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.

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

#3


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

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

#1


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

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

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

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

#2


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.

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

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

这就是IEquatable接口的用武之地.IEququatable是.NET 2.0中的一个新的通用接口,它允许你像System.Object.Equals方法一样做,但不必执行强制转换。因此,在实现此接口时,您可以减少强制转换的数量,这对性能来说是一件好事。特别是在使用泛型集合时,由于泛型集合在其某些方法(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.

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

#3


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

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