C#实施IEquatable<;T>;。相等<;T>;
我有一门课是这样的:C#实施IEquatable<;T>;。相等<;T>;,c#,generics,interface,C#,Generics,Interface,我有一门课是这样的: public class Foo<T> : IEquatable<T> where T : struct { List<T> lst; [Other irrelevant member stuff] } 公共类Foo:IEquatable其中T:struct { 列表lst; [其他不相关的会员资料] } 我想为Foo类实现IEquatable接口。我需要做什么。为了简单起见,我只想检查列表成员是否相等 谢谢 允许使用
public class Foo<T> : IEquatable<T> where T : struct
{
List<T> lst;
[Other irrelevant member stuff]
}
公共类Foo:IEquatable其中T:struct
{
列表lst;
[其他不相关的会员资料]
}
我想为Foo类实现IEquatable
接口。我需要做什么。为了简单起见,我只想检查列表成员是否相等
谢谢
允许使用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));
}
public bool Equals(Foo-Foo)
{
返回lst等于(foo.lst);
}
公共覆盖布尔等于(对象对象对象)
{
if(obj==null)返回base.Equals(obj);
如果(!(对象是Foo))
{
抛出新异常(“obj”参数不是Foo对象。”);
}
其他的
{
返回等于(对象为Foo)
}
}
公共覆盖int GetHashCode()
{
返回此.lst.GetHashCode();
}
公共静态布尔运算符==(Foo f1,Foo f2)
{
返回f1。等于(f2);
}
公共静态布尔运算符=(富一代,富二代)
{
返回(!f1.等于(f2));
}
我得到这个错误:
Error 1 'Foo<T>' does not implement interface member 'System.IEquatable<T>.Equals(T)
错误1“Foo”未实现接口成员“System.IEquatable.Equals(T)
试试这个
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);
}
}
公共类Foo:IEquatable其中T:struct
{
列表lst;
#区域可容纳成员
公共布尔等于(Foo其他)
{
if(lst.Count!=其他.lst.Count)
{
返回false;
}
对于(int i=0;i
不幸的是,列表
没有覆盖等于
或GetHashCode
。这意味着即使更正了类声明,也需要自己执行比较:
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 bool Equals(Foo-Foo)
{
//如果您正在重载,则需要调用ReferenceEquals==
if(foo==null)
{
返回false;
}
如果(foo==这个)
{
返回true;
}
//我假设列表永远不能为空
if(lst.Count!=foo.lst.Count)
{
返回false;
}
对于(int i=0;i
在重载==和!=之前,我个人会仔细考虑也如果您决定实现它们,您应该考虑其中一个或两个值都为null的情况:
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);
}
公共静态布尔运算符==(Foo f1,Foo f2)
{
if(object.ReferenceEquals(f1,f2))
{
返回true;
}
如果(object.ReferenceEquals(f1,null))//f2=null被等于覆盖
{
返回false;
}
返回f1。等于(f2);
}
谢谢。我需要一个IEQUATABLE这个列表不行。Equals不会做你想做的。谢谢!我想知道如何克服“两个值都为null”的情况。答案是object.ReferenceEquals()。谢谢