public class Employee { private string employeeName; private int employeeNumber; public Employee(string employeeName, int employeeN { this.employeeName = employeeName; this.employeeNumber = employeeNumber; } public override bool Equals(object obj) { if (obj == null) return false; Employee other = obj as Employee; if (other == null) return false; if (this.employeeNumber == other.employeeNumbe return true; else return false; } public bool Equals(Employee obj) { if (obj == null) return false; if (this.employeeNumber == obj.employeeNumber) return true; else return false; } }We wrote some tests, the ones we are interested in this post are
[TestMethod] public void RhonaIsRhondaTest() { Assert.AreEqual(rhona, rhonda); } [TestMethod] public void RhonaIsRhondaUsingEqualsTest() { Assert.IsTrue(rhona.Equals(rhonda)); }Both methods pass. However, the first test (using Assert.AreEqual) will execute the Equals method with an object as parameter - the one that overrides the virtual method in System.Object.
In our next test we are going to put two of our employees into a List (andrew and rhonda) and then see if rhona is in the list? Which it should be?
[TestMethod] public void ContainsInListTest() { ListThe test passes - but if you then run through the debugger it is calling the Equals method inherited from System.Object. Let us now have our Employee class implement the interface IEquatable (or IEquatable<Employee>). This interface is defined asworkers = new List<Employee>(); workers.Add(andrew); workers.Add(rhonda); Assert.IsTrue(workers.Contains(rhona)); }
public interface IEquatable<T> { bool Equals(T other); }So the method we have created for Equals(Employee obj) matches this signature. Check the three tests above still pass. The only test that has changed is the call to ContainsTo in the list - which now calls the method as defined by IEquatable. This interface is used by generic collections (such as List).
The next post will look at GetHashCode() and using a HashSet<T>.
No comments:
Post a Comment