public class ChangeRequests : IXmlSerializable, ICloneable, IEnumerable<ChangeRequest>, IEquatable<ChangeRequests> { ... } public class ChangeRequest : ICloneable, IXmlSerializable, IEquatable<ChangeRequest> { ... }
我试图做两个ChangeRequests实例的联合.但是,似乎没有删除重复项.我的MSTest单元测试如下:
var cr1 = new ChangeRequest { CRID = "12" }; var crs1 = new ChangeRequests { cr1 }; var crs2 = new ChangeRequests { cr1.Clone(), new ChangeRequest { CRID = "34" } }; Assert.AreEqual(crs1[0], crs2[0], "First CR in both ChangeRequests should be equal"); var unionedCRs = new ChangeRequests(crs1.Union<ChangeRequest>(crs2)); ChangeRequests expected = crs2.Clone(); Assert.AreEqual(expected, unionedCRs, "Duplicates should be removed from a Union");
测试在最后一行失败,unionedCRs包含两个cr1副本.当我尝试调试并逐步执行每一行时,我在第一行的ChangeRequest.Equals(object)中以及ChangeRequest.Equals(ChangeRequest)的第一行中有一个断点,但都没有被击中.为什么联合包含重复的ChangeRequest实例?
编辑:根据要求,这里是ChangeRequests.Equals(ChangeRequests):
public bool Equals(ChangeRequests other) { if (ReferenceEquals(this, other)) { return true; } return null != other && this.SequenceEqual<ChangeRequest>(other); }
这里是ChangeRequests.Equals(对象):
public override bool Equals(object obj) { return Equals(obj as ChangeRequests); }
编辑:我在ChangeRequest和ChangeRequests上覆盖GetHashCode,但仍然在我的测试中,如果我做IEnumerable< ChangeRequest> unionedCRsIEnum = crs1.Union< ChangeRequest>(crs2);, unionedCRsIEnum以CRID 12的两个ChangeRequest副本结束.
编辑:因为Assert.AreEqual(期望,unionedCRs.Distinct(),“Distinct应该删除重复”),某些地方必须使用我的Equals或GetHashCode实现.失败了,expect和unionedCRs.Distinct()的字符串表示显示unionedCRs.Distinct()肯定有两个CR 12副本.
确保您的GetHashCode实现与您的Equals一致 – Enumerable.Union方法似乎确实使用了两者.如果你已经实现了一个而不是另一个,你应该从编译器得到警告;你仍然需要确保两种方法相互一致.以下是规则的便捷摘要:Why is it important to override GetHashCode when Equals method is overridden?