我有两个具有完全相同引用元素的枚举,想知道为什么 Equals 不成立。
作为附带问题,下面比较每个元素的代码有效,但必须有更优雅的方法
var other = (ActivityService) obj;
if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false;
for (int i = 0; i < AllAccounts.Count(); i++) {
if (!AllAccounts.ElementAt(i).Equals(other.AllAccounts.ElementAt(i))) {
return false;
}
}
return true;
最佳答案
看看 Enumerable.SequenceEqual method .
bool result = AllAccounts.SequenceEqual(other.AllAccounts);
根据数据类型,您可能还需要使用 overloaded method接受 IEqualityComparer
来定义自定义比较方法。
关于c# - 两个可枚举之间的相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2564983/