• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Advanced » Operator Overloading in C#
Next →
← Prev

Operator Overloading in C#

By Dinesh Thakur

Another important and exciting feature object-oriented programming is Operator overloading. C# supports the concept of operator overloading.

Operator overloading is a concept in which operator can defined to work with the userdefined data types such as structs and classes in the same way as the pre-defined data types.

Consider an example:

 

Overloading on ‘+’ operator

 

using System;

public class Item

{

pub1ic int i;

public Item( int j)

{

i = j;

}

public static Item operator + ( Item x , Item y)

-{

system.console.writeLine(“operator +” + x.i +” “+ y.i);

Item z = new Item(x.i+y.i);

return z;

}

}

public class Test

{

public static void Main()

{

Item a = new Item(l0);

Item b = new Item(5);

Item c;

c = a + b ;

system.console.writeLine(c.i);

}

}

OUTPUT:

Operator + 10 5

15

The word operator as the name of a function, is legal and the only way to overload operators. We follow this word with the operator we want to overload and then the parameters we will call the operator with. + is a binary operator and will need two Item’s, one on its left and the other on its right. Then at beginning, we give the return value. of the operator. In our case we want a + to add two Item’s and return a third Item where i will be the sum of the individual i’s. Thus a+b will call the operator + with x being equal to a and y to b. Thus x.i will have a value 10 and y.i, 5. We are creating a new object z and in the constructor passing 15 i.e. 10 + 5. Thus the i of z will be 15 which is being returned. a + b will now be replaced by the object whose i has a value 15 and c will be equal to this object. Thus c.i will be equal to 15.

There are many operators that cannot be overloaded. Which are listed below:

• Conditional Operator                    & &, II

• Compound Assignment                +=, -=, *=, 1=, %=

• Other Operators                            [], 0, = , ?:, ->, new, sizeof, typesof, is, as

Multiple Calling of Overloaded Operator.

 

using system;

public class Item

{

pub1ic int i;

public Item( int j)

{

i = j;

}

public static Item operator + ( Item x , Item y)

{

system.console.writeLine(“operator +” + x.i +u ” + y.i);

Item z = new Item(x.i+y.i);

return z;

}

}

public class Test

{

public static void Main()

{

Item a = new Item(lO);

Item b = new Item(5);

Item c = new Item(2);

Item d;

d = a’+ b + c ;

system.Console.writeLine(d.i);

}

}

OUTPUT:

Operator + 10 5

Operator + 15 2

17

There is only change d = a + b + c from above example. C# gets easily confused with complex statements so it does not read all of it. It sees two operators on the same line. In this case, the same plus. An internal rule tells it to read the plus left to right i.e. it will only see a + b. It will call the operator + with x.i as 10 and y.i as 5 because a’s i is 10 and obi’s i is 5. This will create a temporary object like Item whose i is 15, lets call it zz. The object z is very different from zz. C# then evaluates zz + c. Thus x.i will display 15 and y.i will have the value of c.i i.e. 2. To support multiple invocations of the operator on a single line, the code does not change.

You’ll also like:

  1. What is Function Overloading and Operator Overloading
  2. Operator Overloading in C++
  3. C++ Program Illustrates The Overloading Of Unary Minus (-) Operator
  4. Write a C++ program to Operator Overloading Using a Friend Function.
  5. Write A C++ Program Illustrates The Overloading The Increment Operator (++) With Return Value.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW