Redefining Equality by Overriding Equals


Equality relates to the values of the fields: Are two variables pointing to objects that have the same values in their fields? For example, two account objects might be equivalent if the objects have the same account number and the same balance. Why is this necessary? Suppose you have an array of Person objects. Each Person has a name and an age. If you want to search the array for a particular person, the system has to know what values to use for the comparison. There is no way the system can test arbitrary fields for comparison. Instead, the .NET framework lets you define what equality means yourself. This is done by overriding two methods from the class System.Object: Equals() and GetHashCode() .

To override the Equals and GetHashCode functions:

  1. Type public override bool Equals(object obj) .

  2. Type an open curly bracket { .

  3. If obj is pointing to the same object as this object then return true. For example: if (System.Object. ReferenceEquals(obj,this)) return true; .

  4. If the variable obj and the reference to this are not identical, then cast obj to the class where you are adding the Equals function. For example:

     Person temp = (Person) obj;. 
  5. Test the fields in obj to the fields in this instance of the class. For example:

     if (temp.name == this.name && temp.age == temp.age) 
  6. Return true if the fields are equal, otherwise return false.

  7. Type a close curly bracket } .

  8. Type public override int GetHashCode() .

  9. Type an open curly bracket { .

  10. Type return fld1.GetHashCode() + fld2.GetHashCode() + ... where fld1 and fld2 are the fields that you used for determining equivalence in step 5. For example: return this.name.GetHashCode() + this.age.GetHashCode();.

  11. Type a close curly bracket } ( Figure 6.22 ).

    Figure 6.22 The == operator can only be overridden in C#, not in VB.NET, so Equals is a standard way for all languages to define equivalence. When you override Equals you have to override GetHashCode as well.
     class Person {    string name;    string age;    public Person(string Name, string Age)    {       name = Name;       age = Age;    }  public override bool Equals(   object obj)  {       if (System.Object.ReferenceEquals(       obj,this)) return true;       Person temp = (Person) obj;       if (temp.name == this.name &&           temp.age == this.age)           return true;       else           return false;    }  public override int GetHashCode()  {       return this.name.GetHashCode() +       this.age.GetHashCode();    } } 

graphics/tick.gif Tip

  • Whenever you override the Equals() function, you have to override the GetHashCode() function. A hash code is a numeric representation of the data in the class. Hash codes are used by the .NET Framework to group objects in tables like HashTables and dictionaries. You can return any integer number as the result of GetHashCode() . However, the only criteria is that if two objects return true for Equals they should return the same hash code. So it's a good idea to base the hash code on the fields that were used to test for equivalence.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net