Redefining Equality by Overriding


Redefining Equality by Overriding ==

You can also redefine equality in your class by overriding the == operator and the != operator. If you override the == operator the compiler warns you that you should also override the Equals function from System.Object. Also, whenever you override the == operator, the compiler forces you to override != .

To override the == operator:

  1. Type public static bool operator == (.

  2. Type ClassName one where ClassName is the name of the class where you are adding this function and one is a variable to hold the first object in the comparison.

  3. Type a comma , .

  4. Type ClassName two where ClassName is the name of the class where you are adding this function and two is a variable to hold the second object in the comparison. For example: Checking one, Checking two .

  5. Type a close parenthesis ) .

  6. Type an open curly bracket { .

  7. Type return one.Equals(two); .

  8. Type a close curly bracket } ( Figure 6.20 ).

    Figure 6.20 The == operator usually reports whether two objects are actually identical in memory. We're overriding == to report equality. Two objects are equivalent if they are of the same type and their fields have the same values.
     class Rectangle {    int x,y,width,height;    public Rectangle(int x2,                    int y2,                    int width2,                    int height2)    {       x=x2;y=y2;       width=width2;height=height2;    }  public static bool operator ==   (Rectangle one, Rectangle two)   {   if (one.x == two.x &&   one.y == two.y &&   one.width == two.width &&   one.height == two.height)   return true;   else   return false;   }  } 

To override the != operator:

  1. Type public static bool operator != ( .

  2. Type ClassName one where ClassName is the name of the class where you are adding this function and one is a variable to hold the first object in the comparison.

  3. Type a comma , .

  4. Type ClassName two where ClassName is the name of the class where you are adding this function and two is a variable to hold the first object in the comparison. For example: Checking one, Checking two .

  5. Type a close parenthesis ) .

  6. Type an open curly bracket { .

  7. Type return !(one == two); .

  8. Type a close curly bracket } ( Figure 6.21 ).

    Figure 6.21 If you override the == operator, the C# compiler forces you to override the != operator as well. Most developers just return the opposite of the == operator as illustrated above.
     class Rectangle {    int x,y,width,height;    public Rectangle(int x2,                    int y2,                    int width2,                    int height2)    {       x=x2;y=y2;       width=width2;height=height2;    }    public static bool operator ==    (Rectangle one, Rectangle two)    {       if (one.x == two.x &&           one.y == two.y &&           one.width == two.width &&           one.height == two.height)          return true;       else          return false;    }  public static bool operator !=   (Rectangle one, Rectangle two)   {   return !(one == two);   }  } 

graphics/tick.gif Tips

  • Your implementation of these functions may vary but if you override the == operator, you should really override the Equals() function. Because the Equals() function and the == operator should base their decisions on the same criteria, I have chosen to just return the result of Equals as the result of == . Also I have chosen to return the opposite of == in != . Other classes in the framework may choose to duplicate the code for each function in order to gain in performance. It is sometimes faster to duplicate a little bit of code than to have one function that calls another, which calls a third. Calling functions also has a little overhead.

  • The override of the == operator is only beneficial when your class is to be used strictly inside C#. If you were to use the class from another language like Visual Basic.NET for example, the VB program wouldn't be able to use the == operator and would just have to rely on the implementation of Equals . For this reason, and because == sometimes means identity (if it isn't overridden) and sometimes means equivalence, programmers prefer to not override == and instead override only the Equals() function.




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