BlackWaspTM
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.

10 November 2007