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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More