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# Basic Operator Overloading

The eighth article in the C# Object-Oriented Programming tutorial describes a third overloading technique. By overloading the functionality of operators, the operation of the standard operators including + and - can be defined for new classes.

Operator Overloading

So far in this tutorial we have created classes to represent real-world objects complete with their appropriate methods and properties. These objects have not required the implementation of arithmetic operators as this type of functionality was not appropriate. In this article, we will create a class that does support arithmetic operations through the use of operator overloading.

Operator overloading is simply the process of adding operator functionality to a class. This allows you to define exactly how the operator behaves when used with your class and other data types. This can be standard uses such as the ability to add the values of two vectors, more complex mathematics for multiplying matrices or non-arithmetic functions such as using the + operator to add a new item to a collection or combine the contents of two arrays. Multiple overloaded versions of operators may also be created to provide different functionality according to the data types being processed, in a similar manner to the varying signatures of method overloading.

In this article we will create a new class to represent a two-dimensional vector with X and Y properties. We will use this class in this article and future articles to demonstrate operator overloading. To start, create a new console application named "VectorDemo" and add a new class file named "Vector". Add the following code to the new class to create the properties and a basic constructor:

private int _x, _y;

public Vector(int x, int y) { _x = x; _y = y; }

public int X
{
    get { return _x; }
    set { _x = value; }
}

public int Y
{
    get { return _y; }
    set { _y = value; }
}

Binary Operator Overloading

The first type of operator to consider is the binary operator, so named because they require two values to work with. These include the simple arithmetic operators such as +, -, *, / and %. To declare a binary operator, the following syntax is used:

public static result-type operator binary-operator (
     op-type operand,
     op-type2 operand2
)

This initially appears to be a rather complex declaration but in fact is quite simple. The declaration starts with public static as all operators must be declared as such. Other scopes are not permitted and neither are non-static operators.

The result-type defines the data type or class that is returned as the result of using the operator. Usually this will be the same type as the class that it is being defined within. However, that need not be the case and it is perfectly valid to return data of a different type.

The operator keyword is added to tell the compiler that the following binary-operator symbol is an operator rather than a normal method. This operator will then process the two operand parameters, each prefixed with its data type (op-type and op-type2). As least one of these operands must be the same type as the containing class.

Creating the Addition (+) Operator

The syntax for binary operators can now be used to create a new addition operator for the Vector class. This operator will simply add the X and Y elements of two vectors together and return a new vector containing the result. Add the following to the Vector class to provide this functionality. Note that a new Vector is created rather than adjusting one of the operands. This is because the operands are reference-types and the original values should not be updated in this case.

public static Vector operator +(Vector v1, Vector v2)
{
    return new Vector(v1.X + v2.X, v1.Y + v2.Y);
}

We can now test the Vector's new operator by modifying the program's main method. The following program instantiates two Vector objects, adds them together and outputs the values of the resultant Vector's X and Y properties.

static void Main(string[] args)
{
    Vector v1 = new Vector(4, 11);
    Vector v2 = new Vector(0, 8);

    Vector v3 = v1 + v2;

    Console.WriteLine("({0},{1})", v3.X, v3.Y);     // Outputs "(4,19)"
}

Creating the Subtraction (-) Operator

Addition is a commutative operation. This means the order of the two operands can be swapped without affecting the outcome. In the case of subtraction this is not the case so it important to remember that the first operand in the declaration represents the value to the left of the operator and the second operand represents the value to the right. If these are used incorrectly, the resultant value will be incorrect. Using this knowledge we can add a subtraction operator to the Vector class:

public static Vector operator -(Vector v1, Vector v2)
{
    return new Vector(v1.X - v2.X, v1.Y - v2.Y);
}

To test the new operator, modify the Main method as follows and execute the program.

static void Main(string[] args)
{
    Vector v1 = new Vector(4, 11);
    Vector v2 = new Vector(0, 8);

    Vector v3 = v1 - v2;

    Console.WriteLine("({0},{1})", v3.X, v3.Y);     // Outputs "(4,3)"
}
10 November 2007