News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

C# Tutorial – 23 – Operator Overloading

Operator overloading allows operators to be redefined and used where one or both of the operands are of a certain class. When done correctly, this can simplify the code and make user-defined types as easy to use as the simple types.

Operator overloading example

In this example, there is a class called MyNum with an integer field and a constructor for setting that field. There is also a static Add method that adds two MyNum objects together and returns the result as new MyNum object.

class MyNum
{
  public int val;
  public MyNum(int i) { val = i; }
 
  public static MyNum Add(MyNum a, MyNum b) {
    return new MyNum(a.val + b.val);
  }
}

Two MyNum instances can be added together using the Add method.

MyNum a = new MyNum(10), b = new MyNum(5);
MyNum c = MyNum.Add(a, b);

Binary operator overloading

What operator overloading does is to simplify this syntax and thereby provide a more intuitive interface for the class. To convert the Add method to an overload method for the addition sign, replace the name of the method with the operator keyword followed by the operator that is to be overloaded. The whitespace between the keyword and the operator can optionally be left out. Note that for an operator overloading method to work, it must be defined as both public and static.

class MyNum
{
  public int val;
  public MyNum(int i) { val = i; }
 
  public static MyNum operator +(MyNum a, MyNum b) {
    return new MyNum(a.val + b.val);
  }
}

Since the class now overloads the addition sign this operator can be used to perform the required calculation.

MyNum a = new MyNum(10), b = new MyNum(5);
MyNum c = a + b;

Unary operator overloading

Addition is a binary operator, because it takes two operands. To overload a unary operator, such as increment (++), a single method parameter is used instead.

public static MyNum operator ++(MyNum a) 
{ 
  return new MyNum(a.val + 1); 
}

Note that this will overload both the postfix and prefix versions of the increment operator.

MyNum a = new MyNum(10);
a++;
++a;

Return types and parameters

When overloading a unary operator the return type and parameter type must be of the enclosing type. On the other hand, when overloading most binary operators the return type can be anything, except for void, and only one of the parameters must be of the enclosing type. This means that it is possible to further overload a binary operator with other method parameters, for example to allow a MyNum and an int to be added together.

public static MyNum operator +(MyNum a, int b) 
{
  return new MyNum(a.val + b); 
}

Overloadable operators

C# allows overloading of almost all operators, as can be seen in the table below. The combined assignment operators cannot be explicitly overloaded. Instead, they are implicitly overloaded when their corresponding arithmetic or bitwise operators are overloaded.

Binary operatorsUnary operatorsNot overloadable
+ – * / % (+= -= *= /= %=)
& | ^ << >> (&= |= ^= <<= >>=)
== != > < >= <=
+ – ! ~ ++ — true false&& || = . ( ) :: ?: new as is typeof checked unchecked

The comparison operators, as well as true and false, must be overloaded in pairs. For example, overloading the equal operator means that the not equal operator also has to be overloaded.

True and false operator overloading

Notice in the previous table that true and false are considered to be operators. By overloading them, objects of the class can be used in conditional statements where the object needs to be evaluated as a Boolean type. When overloading them the return types must be bool.

class MyNum
{
  public int val;
  public MyNum(int i) { val = i; }
 
  public static bool operator true(MyNum a) { 
    return (a.val != 0); 
  }
 
  public static bool operator false(MyNum a) { 
    return (a.val == 0); 
  }
}
 
class MyClass 
{
  static void Main() 
  {
    MyNum a = new MyNum(10);
    if (a) System.Console.WriteLine("object is true");
    else   System.Console.WriteLine("object is false");
  }
}