List of Overloadable Operators in C# (C Sharp)

Operator overloading is an interesting feature of Object Oriented Programming. Operator overloading is an example of polymorphism. In general, each operator has a definite meaning.



For example, + operator is used to add two numbers or concatenate two strings by taking two numbers or two strings as arguments correspondingly. Can this operator (+) be used with two class instances as parameters? No. This will end up in error. However you can make it possible by overloading + operator and defining a different meaning for it. This is demonstrated with relevant code in the later part of this article.

Can you overload any operator of your choice? No. You cannot overload all the operators of C#. Only limited set of operators are eligible for overloading. They are listed below:

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

While overloading comparison operators, ensure the following:

• You should overload != operator if you overload == operator and vice versa
• You should overload > operator if you overload < operator and vice versa
• You should overload >= operator if you overload <= operator and vice versa

How can an operator be overloaded? To understand that, consider the + operator discussed in the beginning of this article. You are now going to overload this operator to operate on two class instances as shown below:

class sampleClass {
int member1;
public sampleClass(int member1) {
this.member1 = member1;
}
public static sampleClass operator +(sampleClass instance1, sampleClass instance2) {
int data1 = instance1.member1 * instance2.member1;
sampleClass instance3 = new sampleClass(data1);
return instance3;
}
public int getMember1() {
return member1;
}
}
class testClass {
public static void Main() {
sampleClass instance1 = new sampleClass(10);
sampleClass instance2 = new sampleClass(30);
sampleClass instance3 = instance1 + instance2;
int data1 = instance3.getMember1();
Console.WriteLine(“ Instance3 member value is: {0}”, data1);
}
}

Output of this code will be:

Instance3 member value is: 300

You achieved operator overloading by defining a static method using the keyword “operator” along with the operator + that has to be overloaded. Similarly, you can overload all other overloadable operators listed in this article.

| Design Guideline for C# Structs (C Sharp) | Design Patterns – Its Importance and Types | How to Implement Proxy Pattern Using C# (C Sharp)? | How to Implement Singleton Pattern Using C# (C Sharp)? | Illustration of Abstract Classes of C# (C Sharp) with Examples | Illustration of Sealed Classes of C# (C Sharp) with Examples | List of Overloadable Operators in C# (C Sharp) | Usage of [ ] and () Operators in C# (C Sharp) | What is C# (C Sharp) Nested Type? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.