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: -
Type public static bool operator == (. -
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. -
Type a comma , . -
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 . -
Type a close parenthesis ) . -
Type an open curly bracket { . -
Type return one.Equals(two); . -
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: -
Type public static bool operator != ( . -
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. -
Type a comma , . -
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 . -
Type a close parenthesis ) . -
Type an open curly bracket { . -
Type return !(one == two); . -
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); } } 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. |