Introduction
In this article we will learn operator overloading and the basic concepts of operator overloading. In the contrast operator overloading works on the operator, where we can expand its usages to our class and effects of the operator. It comes under our control and it might differ from class to class. Microsoft includes 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 declares 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 in semantic convenience, readability and maintainability.
Operator overloading is closely related to method overloading. For overloading an operator, we use the operator keyword to define a 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)
- {
-
- }
Binary Operator syntax
- public static return-type operator op(param-type1 operand1,param-type1 operand2)
- {
-
- }
- 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 OperatorOverloading
- {
- 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 oj2)
- {
- Program result = new Program();
- result.x = obj1.x + oj2.x;
- result.y = obj1.y + oj2.y;
- result.z = obj1.z + oj2.z;
- return result;
- }
- public static Program operator -(Program obj1, Program oj2)
- {
- Program result = new Program();
- result.x = obj1.x - oj2.x;
- result.y = obj1.y - oj2.y;
- result.z = obj1.z - oj2.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, onjB and objC");
- objC.show();
- Console.WriteLine();
-
- objCobjC = objC - objA;
- Console.WriteLine("Subtracting objC - objA");
- objC.show();
- Console.WriteLine();
-
- objCobjC = objC - objB;
- Console.WriteLine("Subtracting obhC - objB");
- objC.show();
- Console.WriteLine();
- Console.Read();
-
- }
- }
- }
Summary
In this article we learned 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.