BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Relational Operator Overloading

The eleventh article in the C# Object-Oriented Programming tutorial furthers the investigation of operator overloading. This time the overloading of the relational operators is described, allowing custom classes to be included in comparison operations.

The GetHashCode Method

The GetHashCode method is another member that must be considered when overloading the equality operator for a class. This method uses a hashing algorithm to generate an integer hash code for an object. The hash code generated can be used for many purposes including within a Hashtable collection.

The default implementation for the GetHashCode method generates a hash code value that can be different for two functionally equivalent objects. As with the Equals method, it is usual that when overloading the equality operator you will want to override the GetHashCode method. The hash codes produced can then be based upon the value of the underlying object rather than the object reference.

NB: Warning messages are produced by the compiler when the equality operator is overloaded but the Equals and GetHashCode methods are not overridden. These warnings should not be ignored but will not prevent the code from being compiled or executed.

Overloading the Comparison Operators

The comparison operators are overloaded using the same basic syntax as the equality operator. Again, the operators must be overloaded in pairs. ie, if < is overloaded then so must be > and if <= is overloaded >= must be also.

Overloading the Comparison Operators in the Vector Class

To determine which of two Vector objects is the larger or smaller, the lengths of the two vectors will be compared. To simplify this, we will first add a new, read-only property to the Vector class. This property will use the Pythagorean theorem to calculate the length of the vector.

Add the new property to the Vector class as follows. This code uses the System.Math library for mathematical functions so ensure that the start of the code file includes a using System; statement.

public double Length
{
    get { return Math.Sqrt(_x * _x + _y * _y); }
}

Now that we have a Length property to facilitate comparison of Vectors, we can define the four remaining relational operators. Each of the operators will perform a comparison of the lengths of the two vectors passed as operands and return a Boolean result. Add the following code to the Vector class to overload the operators:

public static bool operator >(Vector v1, Vector v2)
{
    return (v1.Length > v2.Length);
}

public static bool operator <(Vector v1, Vector v2)
{
    return (v1.Length < v2.Length);
}

public static bool operator >=(Vector v1, Vector v2)
{
    return (v1.Length >= v2.Length);
}

public static bool operator <=(Vector v1, Vector v2)
{
    return (v1.Length <= v2.Length);
}

You can now test the new operators by adjusting the Main method of the program as follows:

static void Main(string[] args)
{
    Vector v1 = new Vector(3, 4);
    Vector v2 = new Vector(4, 3);
    Vector v3 = new Vector(3, 5);

    Console.WriteLine(v1 > v2);                 // Outputs "False"
    Console.WriteLine(v1 < v2);                 // Outputs "False"
    Console.WriteLine(v1 >= v2);                // Outputs "True"
    Console.WriteLine(v1 <= v2);                // Outputs "True"
    Console.WriteLine(v1 > v3);                 // Outputs "False"
    Console.WriteLine(v1 < v3);                 // Outputs "True"
    Console.WriteLine(v1 >= v3);                // Outputs "False"
    Console.WriteLine(v1 <= v3);                // Outputs "True"
}
9 December 2007