C# - Operator Overloading

In this article, I will discuss operator overloading in C#. It is very Like Method Overloading . All operator Overloading methods are static methods of the class.

The Complete list of Operators that can be Overloaded are :

  • Unary Operator : +,-,!,~,++,--,true,false
  • Binary Operator :+,-,*,/,%,&,|,^,<<,>>,==,!=,>,<,>=,<=

The operator which cannot be overloaded

=,&&,||,?:,Cheeked, new, typeof, as, and is operator.

Firstly I will discuss (+) sign operator without Programming. Finally I will implement in coding.
Operator overloading (+) sign actually I will play with (+) sign,

  • “Shamim” +”Uddin”=”ShamimUddin”
  • 1 + 2 = 3
  • obj1 + obj2 = obj3

First line I concatenate two string so I got result =”ShamimUddin”. Second line I add two integer values so I got result =3.Third line I create obj3 using obj1 and ob2.

Suppose I have Employee Class. This employee Class has two property as Name and Number,

code

Using (+) Sign like,

sign

First of all I will Implement my Employee Class in coding,

coding  

When the above code is compiled and executed, it produces the following result:

coding

Next Recommended Reading Main advantage of Operator overloading