Web Design that taps into the haromny and vision of your dreams.

C# Operator Overloading

How operator overloading allows additional functionality

Written By on in C#

257 words, estimated reading time 2 minutes.

In C#, operators can be overloaded as well as methods, a technique that allows custom data types to be manipulated in the same way as a normal data type.

Advanced C# Programming Series
  1. C# Advanced Data Types
  2. Using C# Properties and Indexers
  3. C# Event Handling and Delegates
  4. Method Overloading and Overriding in C#
  5. C# Class Abstraction and Encapsulation
  6. C# Aggregation and Advanced Scope Techniques
  7. Polymorphism in C#
  8. Boxing and Unboxing Value Types in C#
  9. C# Operator Overloading
  10. Creating Multi-Threading Applications with C#
  11. Unsafe Code Execution in Microsoft .Net

Let's say you create a bank account class, for simplicity it will only contain a balance and holder name.

public class bankAccount
{
  decimal balance;
  string holdersName;
}
 
public class Program
{
  static void Main()
  {
    bankAccount testAccount1 = new bankAccount();
    bankAccount testAccount2 = new bankAccount();
 
    testAccount1.balance = 10.0;
    testAccount1.holdersName = "Bob Smith";
 
    testAccount2.balance = 20.0;
    testAccount2.holdersName = "Jane Doe";
  }
}

If you wanted to add testAccount2 to testAccount1 you may be tempted to try:

testAccount1 = testAccount1 + testAccount2

or even

testAccount1 += testAccount2

You will find that the compiler will not let you add these together as it does not know how to handle the operators for this custom type. We can tell the C# compiler how to add two bank accounts together by overloading the operators.

public class bankAccount
{
  public decimal balance;
  public string holdersName;
 
  public static bankAccount operator +(bankAccount b1, bankAccount b2)
  {
    bankAccount temp = new bankAccount();
    temp.balance = b1.balance + b2.balance;
    temp.holdersName = b1.holdersName + " and " + b2.holdersName;
    return temp;
  }
}

This will allow the use of the + operator on the bankAccount class. It will return a bankAccount, which contains the sum of the two balances and the two holders names concatenated.

All the other operators can be overloaded in the same way; all you need to do is provide your own logic within the method.

testAccount1 = testAccount1 + testAccount2;
 
// testAccount1.ballance = 30
// testAccount1.holdersName = "Bob Smith and Jane Doe"

Obviously, this is not how bank accounts are merged, but it gives an illustration of how operators can be overloaded for custom data types.

Last updated on: Friday 23rd June 2017

 

Comments
Mini

Mini

Good One.Finally i understood whats operator overloading is.

Reply to Mini
Anonymouse

Anonymouse

Good article!!! Very simple explanation in that even a novice developer will get the hang of the concept easily.

Reply to Anonymouse
Hil Jacla III

Hil Jacla III

Thank you!
its a good way to manipulate custom types in the most straightforward way. Good Job...^^

Reply to Hil Jacla III
Finally...

Finally...

Finally a good explanation of operator overloading - thank you!

Reply to Finally...

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me