Operator Overloading In C#

Introduction

In this article, we will learn operator overloading and the basic concepts of operator overloading. In contrast, operator overloading works on the operator, where we can expand its usage to our class and the effects of the operator. It comes under our control, and it might differ from class to class. Microsoft included operator overloading for C# in 2001.

Fundamental of Operator Overloading

Operator overloading is the ability to use the same operator (like arithmetic operator, +, -, *, /) to do various operations. The operator keywords declare a function specifying what the operator- symbol means when applied to an instance of a class. Operator overloading gives us the ability to user-define implementations of operations where one or both operands are of a user-defined class. This usually helps to work with objects (any class) as a variable with primitive data type. It is used for semantic convenience, readability, and maintainability.

Operator overloading is closely related to method overloading. For overloading an operator, we use the operator keyword to define an operator method that defines the action of the operator relative to its class.

There are two basic forms of the operator method.

  • Unary operator
  • Binary operator

Syntax

Unary Operator syntax.

public static return-type operator op(param-type operand)
{
    // operations
}

Binary Operator syntax.

public static return-type operator op(param-type1 operand1, param-type2 operand2)
{
    // operations
}
  • op: Operator for overloading.
  • return-type: As the name suggested, this is the return type, and it might be any type; whatever we choose,
    the return-type value should be the same type as the class of the operator being overloaded.
  • param-type: It is a parameter value that we are passing through the method using an operand.

Note. The operator parameter must not be a ref or out modifier.

using System;
namespace operator overloading
{
    class Program
    {
        int x, y, z;

        public Program()
        {
            x = y = z = 0;
        }
        public Program(int i, int j, int k)
        {
            x = i;
            y = j;
            z = k;
        }
        public static Program operator +(Program obj1, Program obj2)
        {
            Program result = new Program();
            result.x = obj1.x + obj2.x;
            result.y = obj1.y + obj2.y;
            result.z = obj1.z + obj2.z;
            return result;
        }
        public static Program operator -(Program obj1, Program obj2)
        {
            Program result = new Program();
            result.x = obj1.x - obj2.x;
            result.y = obj1.y - obj2.y;
            result.z = obj1.z - obj2.z;
            return result;
        }
        public void Show()
        {
            Console.WriteLine(x + "," + y + "," + z);
        }
        static void Main(string[] args)
        {
            Program objA = new Program(1, 2, 3);
            Program objB = new Program(25, 22, 10);
            Program objC;
            Console.WriteLine("The value of objA: ");
            objA.Show();
            Console.WriteLine();
            Console.WriteLine("The value of objB: ");
            objB.Show();
            Console.WriteLine();
            objC = objA + objB;
            Console.WriteLine("Result of objA + objB");
            objC.Show();
            Console.WriteLine();
            objC = objA + objB + objC;
            Console.WriteLine("Adding objA, objB, and objC");
            objC.Show();
            Console.WriteLine();
            objC = objC - objA;
            Console.WriteLine("Subtracting objC - objA");
            objC.Show();
            Console.WriteLine();
            objC = objC - objB;
            Console.WriteLine("Subtracting objC - objB");
            objC.Show();
            Console.WriteLine();
            Console.Read();
        }
    }
}

Output

BinaryOverloading

Summary

In this article, we learned about operator overloading, the basic definition and why we use operator overloading in C# programs, what are the significance of operator overloading, and the various types of operator overloading.


Similar Articles