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

.NET 1.1+

The tenth article in the C# Object-Oriented Programming tutorial continues the discussion of operator overloading. In this article, the overloading of the logical operators is described including how to enable the short-circuit Boolean logical operators.

### Creating the Boolean NOT Operator (!)

The NOT operator for the Vector class will examine the contents of the X and Y properties. As described above, if both co-ordinates are zero, the Boolean value for the object is false. However, as this is the NOT operator, we will perform the check and return true only when both values are zero. Add the following code to the class to provide the NOT operator:

```public static bool operator !(Vector v)
{
return ((v.X == 0) && (v.Y == 0));
}```

To test that the operator is working correctly, modify the Main method as follows:

```static void Main(string[] args)
{
Vector v1 = new Vector(0, 0);
Vector v2 = new Vector(10, 0);

Console.WriteLine(!v1);                     // Outputs "True"
Console.WriteLine(!v2);                     // Outputs "False"
}```

## Enabling the Short-Circuit Operators

The short-circuit operators provide an additional variant of the AND and OR operators. In each case, the left-hand operand in the operation is examined in isolation first. When evaluating an AND where the first operand evaluates to false, the result of the operation will be false regardless of the value of the second operand. Similarly, when evaluating an OR operation where the first operand evaluates to true, the result will always be true. In these special cases, the right-hand operand is not evaluated at all, potentially improving performance.

The short-circuit operators cannot be overloaded directly. However, if two conditions are met in the class then the short-circuit operators are automatically made available. The two conditions are as follows:

• The class must overload the normal logical operators (& and |) with the operation returning a value of the same type as the containing class. Each parameter of the operator must also be of the type of the containing class.
• The true and false operators must be overloaded.

When the operator is invoked, the true or false operators are used to determine the status of the first operand. If this guarantees an outcome from the operation, the result is returned immediately. When the state of the first operand does not force an outcome, the AND or OR operator is used for the two values to determine the result.

### Adding the Short-Circuit Operator Pre-Requisites to the Vector Class

The Vector class already includes overloaded true and false operators. To enable the short-circuit operators, we just need to add the correct signature for the AND and OR operators. As these must return a Vector result that can be evaluated as either true or false according to the results, this will return (0,0) if the operation equates to false and (1,1) for true.

Modify the code for the existing & and | operator overloads as follows:

```public static Vector operator &(Vector v1, Vector v2)
{
bool v1flag = !((v1.X == 0) && (v1.Y == 0));
bool v2flag = !((v2.X == 0) && (v2.Y == 0));

if (v1flag & v2flag)
return new Vector(1, 1);
else
return new Vector(0, 0);
}

public static Vector operator |(Vector v1, Vector v2)
{
bool v1flag = !((v1.X == 0) && (v1.Y == 0));
bool v2flag = !((v2.X == 0) && (v2.Y == 0));

if (v1flag | v2flag)
return new Vector(1, 1);
else
return new Vector(0, 0);
}```

You can test that the short-circuit operators are correct by modifying the Main method of the program. However, as the operators do not return a Boolean value, an if statement is required in order to test the results of the operations. This is demonstrated in the following code.

```static void Main(string[] args)
{
Vector v1 = new Vector(0, 0);
Vector v2 = new Vector(10, 0);

if (v1 && v2)
Console.WriteLine("v1 && v2 = true");
else
Console.WriteLine("v1 && v2 = false");  // Outputs "v1 && v2 = false"

if (v1 || v2)
Console.WriteLine("v1 || v2 = true");   // Outputs "v1 || v2 = true"
else
Console.WriteLine("v1 || v2 = false");
}```
26 November 2007