public override bool Equals(object obj) { SimpleObjectWithOverridenEquals o = obj as SimpleObjectWithOverridenEquals; if (o == null) { return(false); } return(A == o.A); }
public void HashSetOfObjectsSimpleObjectWithOverridenEqualsButWithAReferenceEqualityComparerTest() { // Create an object and add it to the hash set SimpleObjectWithOverridenEquals obj = new SimpleObjectWithOverridenEquals { A = 2 }; HashSet <object> hs = new HashSet <object>(ReferenceComparer.Instance); hs.Add(obj); Assert.IsTrue(hs.Contains(obj)); // Modify the object obj.A = 33; Assert.IsTrue(hs.Contains(obj)); // Modified objects will have a different hash code, but they are still found }
public void HashSetOfObjectsSimpleObjectWithOverridenEqualsTest() { // Create an object and add it to the hash set SimpleObjectWithOverridenEquals obj = new SimpleObjectWithOverridenEquals { A = 2 }; HashSet <object> hs = new HashSet <object>(); hs.Add(obj); Assert.IsTrue(hs.Contains(obj)); // Modify the object obj.A = 33; Assert.IsFalse(hs.Contains(obj)); // Modified objects will have a different hash code }