Operator Overloading in C#

I don’t use operator overloading as much in C# as I did in my C++ days. Part of this might simply be down to the types of application I write nowadays where using operating overloading is less useful. Anyway, every time I need to overload an operator I seem to have forgotten the format/syntax. So this post is my memory dump on this subject.

Not all operators can be overloaded. To save myself some typing, check this link for a full list of the operators and what may or may not be overloaded, http://msdn.microsoft.com/en-us/library/8edha89s(v=vs.80).aspx.

Unary Operators

Unary operators, such as +, -, !, ~, ++, –, true, false (yes even true and false) can be overloaded. The syntax is as follows:

public static ReturnType operator Operator(ContainingType)
{
}

For +, -, ++ and — The ReturnType might be the actual object that the code is associated with on another valid type which makes sense for the operation (for example an int).

For the !, ~, true, false the return type must be a boolean.

The Operator is obviously the operator +, -, !, ~, ++, –, true, false and finally the ContainingType must be, well the containing type. You must overload both true and false if you want to overload either of these.

Binary Operators

Binary operators, such as +, -, *, /, %, |, &, ^, << and >> can be overloaded. The syntax is as follows:

public static ReturnType operator Operator(ContainingType, Type)
{
}

The difference being that two arguments are supplied, the two arguments may both be the ContaingType or the second argument might be a different type, for example when using a + b, a might be a complex number and b an int. In the case of the << and >> bit shift operators the second argument must be an int.

Comparison Operators

Comparison operators, such as ==, != <, <=, >, >= can be overloaded. The syntax is as per the binary operator’s syntax. The == and != must both be implemented as pairs of operators as must the > and < operators and the >= and <= operators. Implicit and Explicit operators

If you want to implement cast operators, you can implement an implicit cast should be used if the operation is guaranteed to not lose information and explicit should be used for scenarios where a loss of information is possible and/or where you want to ensure the user is aware of the possible cast.

The syntax for both implicit and explicit cast is the same

public static CastOperator operator CastType(ContainingType)
{
}

The CastOperator is either implicit or explicit, the CastType is the returning type that you’re casting to and the ContainingType is the type that contains the overloaded operator.

For a full, if somewhat pointless example, here’s a Number class that wraps and int

public class Number
{
   private int number;

   public Number(int value)
   {
      number = value;
   }

   public int Value
   {
      get { return number; }
   }

   // format for implicit cast but disabled to ensure it doesn't 
   // affect other operators, i.e. casting to in int automatically.
   //public static implicit operator int(Number n)
   //{
   //	return n.number;
   //}

   public static explicit operator int(Number n)
   {
      return n.number;
   }

   // unary operators
   public static Number operator +(Number n)
   {
      n.number = +n.number;
      return n;
   }

   public static Number operator -(Number n)
   {
      n.number = -n.number;
      return n;
   }

   public static bool operator !(Number n)
   {
      return n.number == 0;
   }

   public static bool operator ~(Number n)
   {
      return !n;
   }

   public static Number operator ++(Number n)
   {
      n.number++;
      return n;
   }

   public static Number operator --(Number n)
   {
      n.number--;
      return n;
   }

   public static bool operator true(Number n)
   {
      return n.number != 0;
   }

   public static bool operator false(Number n)
   {
      return n.number != 0;
   }

   // binary operators
   public static Number operator +(Number a, Number b)
   {
      return new Number(a.number + b.number);
   }

   public static Number operator -(Number a, Number b)
   {
      return new Number(a.number - b.number);
   }

   public static Number operator *(Number a, Number b)
   {
      return new Number(a.number * b.number);
   }

   public static Number operator /(Number a, Number b)
   {
      return new Number(a.number / b.number);
   }

   public static Number operator %(Number a, Number b)
   {
      return new Number(a.number % b.number);
   }

   public static Number operator &(Number a, Number b)
   {
      return new Number(a.number & b.number);
   }

   public static Number operator |(Number a, Number b)
   {
      return new Number(a.number | b.number);
   }

   public static Number operator ^(Number a, int b)
   {
      return new Number(a.number ^ b);
   }

   public static Number operator <<(Number a, int shiftBy)
   {
      return new Number(a.number << shiftBy);
   }

   public static Number operator >>(Number a, int shiftBy)
   {
      return new Number(a.number >> shiftBy);
   }

   // comparison operators
   public static bool operator ==(Number a, Number b)
   {
      return a.Value == b.Value;
   }

   public static bool operator !=(Number a, Number b)
   {
      return a.Value != b.Value;
   }

   public static bool operator <(Number a, Number b)
   {
      return a.Value < b.Value;
   }

   public static bool operator <=(Number a, Number b)
   {
      return a.Value <= b.Value;
   }

   public static bool operator >(Number a, Number b)
   {
      return a.Value > b.Value;
   }

   public static bool operator >=(Number a, Number b)
   {
      return a.Value >= b.Value;
   }
}