C# Operator Overloading3 min read

We have learned that the method can be overloaded in C#, in the same way, we can also overload the operator. We can perform the various operations with the same operator in C#.

Operator overloading means performing different operations with the same operator. We can overload most of the built-in operators that are provided by C#. We can also change the user-defined type as well such as structures or classes.

The keyword ‘operator‘ is used to overload an operator. Compile-time polymorphism is achieved through operator overloading.

Syntax:


The following table shows which operators present in C# can be overloaded and which are not.

OperatorsDescription
+, -, !, ~, ++, – –unary operators take one operand and can be overloaded.
+, -, *, /, %Binary operators take two operands and can be overloaded.
==, !=, =Comparison operators can be overloaded.
&&, ||Conditional logical operators cannot be overloaded directly
+=, -+, *=, /=, %=, =Assignment operators cannot be overloaded.

Also, =, ., ?:, ->, new, is, sizeof, typeof cannot be overloaded.


Overloading Unary Operators

The return type of unary operator overloading can be of any type except the void. The unary operator includes +, ~, ! and dot (.), these must have any return type but void but the return type must be the type of ‘Type’ for ++ and – operators and must be a bool type for true and false operators.

Also, remember that the true and false operators can be overloaded only as pairs. The compilation error occurs if a class declares one of these operators without declaring the other.

The syntax for overloading unary operator:

Let us go through an example in the C# program for overloading unary operators.

Output:

X = 10 Y = 20
X = -10 Y = -20


Overloading Binary Operator

Binary operators are overloaded in pairs such as when an arithmetic operator is overloaded then the corresponding assignment operators also get overloaded automatically. For example, if we overload the + operator then += also gets overloaded automatically.

The syntax for overloading binary operator:
we overload binary operator in the same manner as a unary operator, the only difference is the number of parameters here is two.

Output:

num1 = 20 num2 = 30
num1 = 10 num2 = 20
num1 = 30 num2 = 50


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More