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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …