C#实现IEquatable<T>.Equals<T>

5

我有一个像这样的类:

public class Foo<T> : IEquatable<T> where T : struct
{
    List<T> lst;
    [Other irrelevant member stuff]
}

我想为Foo类实现IEquatable<T>接口。简单起见,我只想检查List成员是否相等。谢谢。
允许使用C# 4.0支持的答案。
更新:以下是我目前拥有的内容:
public bool Equals(Foo<T> foo)
{
    return lst.Equals(foo.lst);
}

public override bool Equals(Object obj)
{
    if (obj == null) return base.Equals(obj);

    if (!(obj is Foo<T>))
    {
        throw new Exception("The 'obj' argument is not a Foo<T> object.");
    }
    else
    {
            return Equals(obj as Foo<T>)
    }
}    

public override int GetHashCode()
{
    return this.lst.GetHashCode();
}

public static bool operator ==(Foo<T> f1, Foo<T> f2)
{
   return f1.Equals(f2);
}

public static bool operator !=(Foo<T> f1, Foo<T> f2)
{
   return (!f1.Equals(f2));
}

我遇到了这个错误:
Error 1 'Foo<T>' does not implement interface member 'System.IEquatable<T>.Equals(T)
2个回答

12

很不幸,List<T>没有覆盖 EqualsGetHashCode。这意味着即使您已经更正了类声明,您仍需要自己执行比较:

public bool Equals(Foo<T> foo)
{
    // These need to be calls to ReferenceEquals if you are overloading ==
    if (foo == null)
    {
        return false;
    }
    if (foo == this)
    {
        return true;
    }
    // I'll assume the lists can never be null
    if (lst.Count != foo.lst.Count)
    {
        return false;
    }
    for (int i = 0; i < lst.Count; i++)
    {
        if (!lst[i].Equals(foo.lst[i]))
        {
            return false;
        }
    }
    return true;
}

public override int GetHashCode()
{
    int hash = 17;
    foreach (T item in lst)
    {
        hash = hash * 31 + item.GetHashCode();
    }
    return hash;
}

public override bool Equals(Object obj)
{
    // Note that Equals *shouldn't* throw an exception when compared
    // with an object of the wrong type
    return Equals(obj as Foo<T>);
}

我个人认为在重载 == 和 != 之前应该仔细考虑。如果你决定实现它们,你应该考虑其中一个或两个值为空的情况:

public static bool operator ==(Foo<T> f1, Foo<T> f2)
{
   if (object.ReferenceEquals(f1, f2))
   {
       return true;
   }
   if (object.ReferenceEquals(f1, null)) // f2=null is covered by Equals
   {
       return false;
   }
   return f1.Equals(f2);
}

谢谢!我想知道你是如何处理 '两个值都为空' 的情况的。object.ReferenceEquals() 是答案。谢谢! - Luke Machowski

2

试试这个。

    public class Foo<T> : IEquatable<Foo<T>> where T : struct
    {
        List<T> lst;

        #region IEquatable<T> Members

        public bool Equals(Foo<T> other)
        {
            if (lst.Count != other.lst.Count)
            {
                return false;
            }

            for (int i = 0; i < lst.Count; i++)
            {
                if (!lst[i].Equals(other.lst[i]))
                {
                    return false;
                }
            }
            return true;
        }

        #endregion

        public override bool Equals(object obj)
        {
            var other = obj as Foo<T>;
            return other != null && Equals(other);
        }


    }

1
谢谢。我需要使用IEquatable<Foo<T>>而不是IEquatable<T>。我知道这很愚蠢。 - Chris
4
这样做行不通 - List.Equals方法不能达到你想要的效果。 - Jon Skeet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接