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+

Overloading True and False in C#

The ninth article in the C# Object-Oriented Programming tutorial continues the discussion of operator overloading. In this article, the overloading of the true and false operators is described, allowing an object to be used in conditional processing.

True and False as Operators

The true and false keywords have many uses in C#. One of these uses is as a pair of operators within a class that allow the class to represent its own state as either true or false. The determination of the result is implemented in rules coded by the programmer. This effectively gives an implicit conversion of a type to a Boolean value that can then be used in conditional processing scenarios such as with the if statement or the conditional operator (?).

This article uses the Vector class developed in the previous article. If you do not have the code, download it using the link above.

Adding True and False Operators

Default Behaviour

The default behaviour of any class is to provide no support for the true and false operators. This means that if an attempt is made to evaluate an object of such a class as a Boolean the code will fail to compile. This can be demonstrated by using a Vector object as the condition in an if statement. Change the Main method of the VectorDemo program as follows and attempt to compile it to see the error.

static void Main(string[] args)
{
    Vector test = new Vector(4, 3);
    if (test)
        Console.WriteLine("True");
    else
        Console.WriteLine("False");
}

Overloading True and False

The syntax for overloading the true and false operators is similar to that of other unary operators. Two limitations exist. Firstly, the return value must be a Boolean. Secondly, it is invalid to overload only one of the two operators; if the true operator is overloaded than so must be false and vice versa.

public static bool operator true(op-type operand)
{
    // Evaluation code
}

public static bool operator false(op-type operand)
{
    // Evaluation code
}

Adding True and False to the Vector Class

The Vector class can now be updated to include overloaded versions of the true and false operators. In the case of vectors, the object will be deemed to be 'true' when either of the vector's X or Y properties is non-zero. If the X and Y values are both zero, the object will evaluate as false. This is simply implemented by adding the following code:

public static bool operator true(Vector v)
{
    if ((v.X != 0) || (v.Y != 0))
        return true;
    else
        return false;
}

public static bool operator false(Vector v)
{
    if ((v.X == 0) && (v.Y == 0))
        return true;
    else
        return false;
}

Testing the Code

Now that the two operators have been added, you should be able to compile and execute the Main method described above. As the vector's X and Y co-ordinates are not zero, the code outputs the text "True". If you modify the Vector's declaration as follows, the code will output false instead.

static void Main(string[] args)
{
    Vector test = new Vector(0, 0);
    if (test)
        Console.WriteLine("True");
    else
        Console.WriteLine("False");         // Outputs "False"
}
15 November 2007