SlideShare a Scribd company logo
OPERATOR OVERLOADING
Ramish Suleman
(IT)
Operator Overloading
Each C# operator has a predefined meaning. Most of them are given
additional meaning through the concept called operator overloading.
Main idea behind operator overloading is to use C# operators with the
class objects.
E.g.
‘+’ can be used to add 2 ints, floats and doubles, the same ‘+’ can be
used to add 2 class objects, thereby ‘+’ gets overloaded.
When an operator is overloaded, its original meaning is not lost.
Operator Overloading
Operator overloading can overload operators to work with class and
struct types.
Use keyword operator.
Follow with symbol
overload+
struct Point
{
int x;
int y;
public static Point operator+(Point p, Point q)
{
return new Point(p.x + q.x, p.y + q.y);
}
...
}
Using Overloaded Operator
Overloaded operator used like operators for other types.
Compiler translates into method call
use operator+
Point a = new Point(1, 2);
Point b = new Point(3, 4);
Point c = a + b;
Types of Operator Overloading
Binary Operators:
Binary operator works with two
parameters.
binary +
binary -
struct Point
{
int x;
int y;
public static Point operator+(Point p, Point q)
{
return new Point(p.x + q.x, p.y + q.y);
}
public static Point operator-(Point p, Point q)
{
return new Point(p.x - q.x, p.y - q.y);
}
...
}
Types of Operator Overloading
Unary Operators:
Unary operator works with single
parameter.
unary +
unary -
struct Point
{
int x;
int y;
public static Point operator+(Point p)
{
return new Point(p.x, p.y);
}
public static Point operator-(Point p)
{
return new Point(-p.x, -p.y);
}
...
}
Advantages of Operator Overloading
With operator overloading readability of the code improves.
With operator overloading the code becomes explicit in nature,
looking at the code, fellow developers can easily guess what is
going on.
With operator overloading the code looks more conventional and
becomes easy to follow.
The method defining operator overloading, should always be
static and public. Otherwise compiler error out with message
"User defined operator must always be static and public".
Limitations
Only some operators can be overloaded
Unary:
+ -! ~ ++ --true false
Binary:
+ -* / % & | ^ << >> == != > < >= <=
Cannot
o Create new operators.
o Change precedence.
o Change associativity.
o Change number of arguments.
o Overload prefix/postfix versions separately.
Summary
There are different ways in which operators may be overloaded
within C#. There are numerous ways that overloading can be used
to your advantage in custom classes and structs. It is now up to you
to decide how to best put it to use in your applications, and
important to remember that not all languages support operator
overloading.
Ad

More Related Content

What's hot (20)

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Burhan Ahmed
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
Sachin Sharma
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Burhan Ahmed
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 

Similar to Operator overloading (20)

Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
Rai University
 
Polymorphism and function overloading_new.ppt
Polymorphism and function overloading_new.pptPolymorphism and function overloading_new.ppt
Polymorphism and function overloading_new.ppt
ChetanyaChopra1
 
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTCBasics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
Mukund Trivedi
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
BalajiGovindan5
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
yatinnarula
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
 
Oops
OopsOops
Oops
ankush_kumar
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
Youth For Peace
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
Week7a.pptx
Week7a.pptxWeek7a.pptx
Week7a.pptx
NasirAli233814
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Garima Singh Makhija
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
Rai University
 
Polymorphism and function overloading_new.ppt
Polymorphism and function overloading_new.pptPolymorphism and function overloading_new.ppt
Polymorphism and function overloading_new.ppt
ChetanyaChopra1
 
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTCBasics _of_Operator Overloading_Somesh_Kumar_SSTC
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
BalajiGovindan5
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
yatinnarula
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
Princess Sam
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
Youth For Peace
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
Ad

Recently uploaded (20)

Wood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City LibraryWood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City Library
Woods for the Trees
 
fundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptxfundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptx
Sunkod
 
Setup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODCSetup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODC
outsystemspuneusergr
 
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvvBasic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
hkthmrz42n
 
kurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptxkurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptx
TayyabaSiddiqui12
 
NASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptxNASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptx
reine1
 
2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx
Dale Wells
 
Speech 2-Unity in Diversity, Strength in Solidarity
Speech 2-Unity in Diversity, Strength in SolidaritySpeech 2-Unity in Diversity, Strength in Solidarity
Speech 2-Unity in Diversity, Strength in Solidarity
Noraini Yunus
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025
Noraini Yunus
 
Bloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptxBloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptx
FamilyWorshipCenterD
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Rajdeep Chakraborty
 
Approach to diabetes Mellitus, diagnosis
Approach to diabetes Mellitus,  diagnosisApproach to diabetes Mellitus,  diagnosis
Approach to diabetes Mellitus, diagnosis
Mohammed Ahmed Bamashmos
 
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdfMicrosoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
MinniePfeiffer
 
The Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdfThe Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdf
RDinuRao
 
A Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity SequencesA Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity Sequences
natarajan8993
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
Bidding World Conference 2027 - Ghana.pptx
Bidding World Conference 2027 - Ghana.pptxBidding World Conference 2027 - Ghana.pptx
Bidding World Conference 2027 - Ghana.pptx
ISGF - International Scout and Guide Fellowship
 
Bidding World Conference 2027 - NSGF Mexico.pdf
Bidding World Conference 2027 - NSGF Mexico.pdfBidding World Conference 2027 - NSGF Mexico.pdf
Bidding World Conference 2027 - NSGF Mexico.pdf
ISGF - International Scout and Guide Fellowship
 
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
patricialago3459
 
Wood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City LibraryWood Age and Trees of life - talk at Newcastle City Library
Wood Age and Trees of life - talk at Newcastle City Library
Woods for the Trees
 
fundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptxfundamentals of communicationclass notes.pptx
fundamentals of communicationclass notes.pptx
Sunkod
 
Setup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODCSetup & Implementation of OutSystems Cloud Connector ODC
Setup & Implementation of OutSystems Cloud Connector ODC
outsystemspuneusergr
 
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvvBasic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
Basic.pptxsksdjsdjdvkfvfvfvfvfvfvfvfvfvvvv
hkthmrz42n
 
kurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptxkurtlewin theory of motivation -181226082203.pptx
kurtlewin theory of motivation -181226082203.pptx
TayyabaSiddiqui12
 
NASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptxNASIG ISSN 2025 updated for the_4-30meeting.pptx
NASIG ISSN 2025 updated for the_4-30meeting.pptx
reine1
 
2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx2025-05-04 A New Day Dawns 03 (shared slides).pptx
2025-05-04 A New Day Dawns 03 (shared slides).pptx
Dale Wells
 
Speech 2-Unity in Diversity, Strength in Solidarity
Speech 2-Unity in Diversity, Strength in SolidaritySpeech 2-Unity in Diversity, Strength in Solidarity
Speech 2-Unity in Diversity, Strength in Solidarity
Noraini Yunus
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025Speech 3-A Vision for Tomorrow for GE2025
Speech 3-A Vision for Tomorrow for GE2025
Noraini Yunus
 
Bloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptxBloom Where You Are Planted 05.04.2025.pptx
Bloom Where You Are Planted 05.04.2025.pptx
FamilyWorshipCenterD
 
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptxBesu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Besu Shibpur Enquesta 2012 Intra College General Quiz Finals.pptx
Rajdeep Chakraborty
 
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdfMicrosoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
Microsoft Azure Data Fundamentals (DP-900) Exam Dumps & Questions 2025.pdf
MinniePfeiffer
 
The Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdfThe Business Dynamics of Quick Commerce.pdf
The Business Dynamics of Quick Commerce.pdf
RDinuRao
 
A Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity SequencesA Bot Identification Model and Tool Based on GitHub Activity Sequences
A Bot Identification Model and Tool Based on GitHub Activity Sequences
natarajan8993
 
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
THE SEXUAL HARASSMENT OF WOMAN AT WORKPLACE (PREVENTION, PROHIBITION & REDRES...
ASHISHKUMAR504404
 
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
ICSE 2025 Keynote: Software Sustainability and its Engineering: How far have ...
patricialago3459
 
Ad

Operator overloading

  • 2. Operator Overloading Each C# operator has a predefined meaning. Most of them are given additional meaning through the concept called operator overloading. Main idea behind operator overloading is to use C# operators with the class objects. E.g. ‘+’ can be used to add 2 ints, floats and doubles, the same ‘+’ can be used to add 2 class objects, thereby ‘+’ gets overloaded. When an operator is overloaded, its original meaning is not lost.
  • 3. Operator Overloading Operator overloading can overload operators to work with class and struct types. Use keyword operator. Follow with symbol overload+ struct Point { int x; int y; public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } ... }
  • 4. Using Overloaded Operator Overloaded operator used like operators for other types. Compiler translates into method call use operator+ Point a = new Point(1, 2); Point b = new Point(3, 4); Point c = a + b;
  • 5. Types of Operator Overloading Binary Operators: Binary operator works with two parameters. binary + binary - struct Point { int x; int y; public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } public static Point operator-(Point p, Point q) { return new Point(p.x - q.x, p.y - q.y); } ... }
  • 6. Types of Operator Overloading Unary Operators: Unary operator works with single parameter. unary + unary - struct Point { int x; int y; public static Point operator+(Point p) { return new Point(p.x, p.y); } public static Point operator-(Point p) { return new Point(-p.x, -p.y); } ... }
  • 7. Advantages of Operator Overloading With operator overloading readability of the code improves. With operator overloading the code becomes explicit in nature, looking at the code, fellow developers can easily guess what is going on. With operator overloading the code looks more conventional and becomes easy to follow. The method defining operator overloading, should always be static and public. Otherwise compiler error out with message "User defined operator must always be static and public".
  • 8. Limitations Only some operators can be overloaded Unary: + -! ~ ++ --true false Binary: + -* / % & | ^ << >> == != > < >= <= Cannot o Create new operators. o Change precedence. o Change associativity. o Change number of arguments. o Overload prefix/postfix versions separately.
  • 9. Summary There are different ways in which operators may be overloaded within C#. There are numerous ways that overloading can be used to your advantage in custom classes and structs. It is now up to you to decide how to best put it to use in your applications, and important to remember that not all languages support operator overloading.