我有一类这样的:C#实现IEquatable <T> .Equal <T>
public class Foo<T> : IEquatable<T> where T : struct
{
List<T> lst;
[Other irrelevant member stuff]
}
我要实现的IEquatable<T>
接口Foo类。我需要做什么。为了简单起见,我只想检查列表成员是否相等。
谢谢。
支持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)
感谢。我需要IEquatable而不是IEquatable 。我知道这是愚蠢的。 –
Chris
2010-01-17 08:28:44
这是行不通的 - List.Equals没有做到你想要的。 – 2010-01-17 08:34:10
感谢您的纠正。修改原始帖子。 – 2010-01-17 10:01:02