16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Objectives Describe equality testing 16: Equals Objectives Describe equality testing Show how to override Object Equals error checking casting chaining to base class comparing data Present static helper method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Equality methods in Object class 16: Equals Equality methods in Object class Object class provides two methods to test equality instance version is primary method static version is convenient helper class Object { public virtual bool Equals(object o) ... public static bool Equals(object a, object b) ... ... } equality methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Object Equals Object class instance Equals compares references does not compare object data performs identity test Student a = new Student("Ann", 32, 8000); Student b = new Student("Ann", 32, 8000); if (a.Equals(b)) ... false, different objects Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Override Can override instance Equals virtual method typical motivation is when distinct objects might be equal class Student { public override bool Equals(object o) ... } ... } override Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Override details Several steps to override instance Equals test for null verify compared objects have same type chain to base class downcast parameter compare data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Test for null Should test parameter against null return false if null 16: Equals Test for null Should test parameter against null return false if null not an error, do not throw exception class Student { public override bool Equals(object o) if (o == null) return false; ... } ... } null? Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Type testing Should verify objects have same type 16: Equals Type testing Should verify objects have same type typical to compare Type objects to require exactly same type not common to use is operator class Student { public override bool Equals(object o) ... if (this.GetType() != o.GetType()) return false; } ... } not same type? Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Downcast and compare Must downcast parameter to access data 16: Equals Downcast and compare Must downcast parameter to access data can choose to compare some or all data class Student { int id; public override bool Equals(object o) ... Student s = (Student)o; return this.id == s.id; } ... } downcast compare data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Inheritance Equals typically chains to base version if provided allows base version to compare base data return false if base parts not equal class Person { string name; int age; public override bool Equals(object o) { ... } ... } base provides Equals method class Student : Person { public override bool Equals(object o) ... if (!base.Equals(o)) return false; } ... } derived version chains to base Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Do not chain to Object instance Equals Do not chain to Object version of Equals Object version performs identity test returns true only if comparing the same objects base class is Object class Person { public override bool Equals(object o) ... } ... } Equals should not chain to base Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Complete implementation 16: Equals Complete implementation Complete code for reference class Student : Person { public override bool Equals(object o) if (o == null) return false; if (this.GetType() != o.GetType()) return false; if (!base.Equals(o)) return false; Student s = (Student)o; return this.id == s.id; } ... } null? not same type? compare base downcast compare data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Static Equals Object provides convenient static version of Equals main service is handling of null references returns false if first argument is null static Equals public static bool Equals(object a, object b) { if (a == b) return true; if (a == null || b == null) return false; return a.Equals(b); } true if same object or both are null delegate to instance Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
GetHashCode and Equals Equal objects must give same hash code if type overrides Equals it must also override GetHashCode Allows efficient containment test in hash tables can hash tested object and search only that bucket Student a = new Student("Ann", 32, 8000); Student b = new Student("Ann", 32, 8000); if (a.Equals(b)) ... if (a.GetHashCode() == b.GetHashCode()) if objects are equal hash codes must be the same Programming C# © 2003 DevelopMentor, Inc. 12/1/2003
Summary Object class provides framework for equality testing 16: Equals Summary Object class provides framework for equality testing instance Equals static Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003