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

#### Creating the Multiplication (*) Operator

The last binary operator that will be added to Vector class is for multiplication. This operator will be used to scale the vector by multiplying the X and Y properties by the same integer value. This demonstrates the use of operands of a different type to the class they are defined within.

```public static Vector operator *(Vector v1, int scale)
{
return new Vector(v1.X * scale, v1.Y * scale);
}```

To test the multiplication operator, adjust the Main method again:

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

Vector v2 = v1 * 3;

Console.WriteLine("({0},{1})", v2.X, v2.Y);     // Outputs "(12,33)"
}```

In the operator code for the multiplication operator, the Vector is the first operand and the integer the second. This means that the order used in the multiplication statement must have the Vector at the left of the operator and the integer value to the right. Changing the order of the operands in the Main method will cause a compiler error.

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

Vector v2 = 3 * v1;

Console.WriteLine("({0},{1})", v2.X, v2.Y);     // Does not compile
}```

If the class must support both variations of multiplication, both must be declared in the code. This provides the benefit of allowing the order of operands change the underlying function. To provide the second variation of multiplication, add the following code to the Vector class. Afterwards, the program will execute correctly.

```public static Vector operator *(int scale, Vector v1)
{
return new Vector(v1.X * scale, v1.Y * scale);
}```

#### Compound Assignment Operators

When an arithmetic binary operator is declared, the corresponding compound assignment operator is also made available to the class. This can be demonstrated by modifying the Main method of the program:

```Vector v1 = new Vector(4, 11);

v1 *= 5;

Console.WriteLine("({0},{1})", v1.X, v1.Y);         // Outputs "(20,55)"```

Unary operators are those that require a single operand. These include the simple increment (++) and decrement (--) operators. To declare a unary operator, the following syntax is used:

public static result-type operator unary-operator (op-type operand)

This syntax is almost identical to that used for binary operators. The difference is that only one operand is declared. The operand type must be the same as the class in which the operator is declared.

#### Creating the Increment and Decrement Operators

Using the syntax defined above, we can now add the increment and decrement operators to the Vector class. Note that there is only a single definition for each. There is no way to differentiate between prefix and postfix versions of the operator so both provide the same underlying functionality.

To declare the two operators, add the following code to the Vector class. Each increments or decrements both the X and Y properties for Vector objects.

```public static Vector operator ++(Vector v)
{
v.X++;
v.Y++;
return v;
}

public static Vector operator --(Vector v)
{
v.X--;
v.Y--;
return v;
}```

To test these operators, update and execute the Main method:

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

v1++;
Console.WriteLine("({0},{1})", v1.X, v1.Y);     // Outputs "(5,12)"

v1--;Console.WriteLine("({0},{1})", v1.X, v1.Y);     // Outputs "(4,11)"
}```

#### Creating the Negation Operator

The last arithmetic unary operator to be considered in this article is the negation operator. This is the unary version of subtraction used to identify a negative version of a value. We can add this operator using the following code:

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

To test the negation operator, update the Main method and run the program.

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

Vector v2 = -v1;
Console.WriteLine("({0},{1})", v2.X, v2.Y);     // Outputs "(-4,-11)"
}```
10 November 2007