C# Fundamentals - Operator Overloading

C# Fundamentals - Operator Overloading

Operator overloading is often times a really handy way of conveying a message as to how the code works.
You most of the built-in operators available in C# can be overloaded / re-defined. Thereby allowing a programmer to use operators with user-defined types as well. Overloaded operators are functions with the keyword operator followed by the symbol for the operator being defined.
Just like any other function, an overloaded operator has a return type and a parameter list.
There are operators than can be overloaded and those that can not.
Can be overloaded : All Unary, All Binary (that take one operand), All Comparison operators.
Can not be overloaded : All Conditional - Logical Operators, All Assignment Operators as well as : ?:, new, sizeof, .(dot) & typeof.

Let us take the example of a Position class, it consists of 3 float variables.
We will first go over overloading the '+' operator to add two positions.
public class Position
{
    public float x,y,z;
    public Position(float x=0, float y=0, float z=0)
    {
         this.x = x;
         this.y = y;
         this.z = z;
    }
    public static Position operator+ (Position A, Position B)
    {
         Position C = new Position();
         C.x = A.x + B.x;
         C.y = A.y + B.y;
         C.z = A.z + B.z;
         return C;
    }
}
    
public class Program
{
   public static void Main(string[] args)
   {
        Position p1 = new Position(10,20,30);
        Position p2 = new Position(20,10,30);
        Position p3 = p1 + p2;
        Console.WriteLine("Position is " + p3.x + ", " + p3.y + ", " + p3.z);
   }
}
Position is 30, 30, 60
Now we will see how to overload comparison operators '>','<' , '==' and '!='
public class Position
{
    public float x,y,z;
    public Position(float x=0, float y=0, float z=0)
    {
         this.x = x;
         this.y = y;
         this.z = z;
    }
    public static bool operator== (Position lhs, Position rhs)
    {
         return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z);
    }
    public static bool operator!= (Position lhs, Position rhs)
    {
         return (lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.x);
    }
    public static bool operator> (Position lhs, Position rhs)
    {
         float lhsSum = lhs.x + lhs.y + lhs.z;
         float rhsSum = rhs.x + rhs.y + rhs.z;
         return (lhsSum > rhsSum);
    }
    public static bool operator< (Position lhs, Position rhs)
    {
         float lhsSum = lhs.x + lhs.y + lhs.z;
         float rhsSum = rhs.x + rhs.y + rhs.z;
         return (lhsSum < rhsSum);
    }
 }
    
 public class Program
 {
     public static void Main(string[] args)
     {
         Position p1 = new Position(20,10,30);
         Position p2 = new Position(20,20,30);
         Console.WriteLine("Is p1 greater than p2 " + (p1 > p2));
         Console.WriteLine("Is p1 less than p2 " + (p1 < p2));
         Console.WriteLine("Is p1 equal to p2 " + (p1 == p2));
     }
 }
Is p1 greater than p2 False
Is p1 less than p2 True
Is p1 equal to p2 False
Now you have a better understanding of how operator overloading works and why it's useful.
Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.
If you have any questions that you might have about shaders, C# or Unity development in general don't be shy and leave a message on my facebook page or down in the comments.
For more C# tutorials go HERE.