C# | Operator Overloading
Last Updated :
07 Aug, 2021
The concept of overloading a function can also be applied to operators. Operator overloading gives the ability to use the same operator to do various operations. It provides additional capabilities to C# operators when they are applied to user-defined data types. It enables to make user-defined implementations of various operations where one or both of the operands are of a user-defined class. Only the predefined set of C# operators can be overloaded. To make operations on a user-defined data type is not as simple as the operations on a built-in data type. To use operators with user-defined data types, they need to be overloaded according to a programmer’s requirement. An operator can be overloaded by defining a function to it. The function of the operator is declared by using the operator keyword.
Syntax :
access specifier className operator Operator_symbol (parameters)
{
// Code
}
Note : Operator overloading is basically the mechanism of providing a special meaning to an ideal C# operator w.r.t. a user-defined data type such as structures or classes.
The following table describes the overloading ability of the various operators available in C# :
Operators |
Description |
+, -, !, ~, ++, – – |
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. |
Overloading Unary Operators
The return type can be of any type except void for unary operators like !, ~, + and dot (.) but the return type must be the type of ‘Type’ for – and ++ operators and must be a bool type for true as well as false operators. But do remember that the true and false operators can be overloaded as pairs only. The compilation error arises if a class declares one of these operators without declaring the other.
The following syntax shows the use of Unary operator –
operator (object);
here, operator is a symbol that denotes a unary operator.
operator a;
Example :
Input : 15, -25
Output : -15, 25
Input : -22, 18
Output : 22, -18
C#
using System;
namespace Calculator {
class Calculator {
public int number1 , number2;
public Calculator( int num1 , int num2)
{
number1 = num1;
number2 = num2;
}
public static Calculator operator -(Calculator c1)
{
c1.number1 = -c1.number1;
c1.number2 = -c1.number2;
return c1;
}
public void Print()
{
Console.WriteLine ( "Number1 = " + number1);
Console.WriteLine ( "Number2 = " + number2);
}
}
class EntryPoint
{
static void Main(String []args)
{
Calculator calc = new Calculator(15, -25);
calc = -calc;
calc.Print();
}
}
}
|
Output :
Number1 = -15
Number2 = 25
Overloading Binary Operators
Binary Operators will work with two Operands. Examples of binary operators include the Arithmetic Operators (+, -, *, /, %), Arithmetic Assignment operators (+=, -+, *=, /+, %=) and Relational Operators etc. Overloading a binary operator is similar to overloading a unary operator, except that a binary operator requires an additional parameter.
Syntax :
operator operator (object1, object2);
Here, second "operator" is a symbol that
denotes a binary operator.
operator + (a, b);
Example :
Input : 200, 40
Output : 240
Input : 300, 20
Output : 320
C#
using System;
namespace BinaryOverload {
class Calculator {
public int number = 0;
public Calculator() {}
public Calculator( int n)
{
number = n;
}
public static Calculator operator + (Calculator Calc1,
Calculator Calc2)
{
Calculator Calc3 = new Calculator(0);
Calc3.number = Calc2.number + Calc1.number;
return Calc3;
}
public void display()
{
Console.WriteLine( "{0}" , number);
}
}
class CalNum {
static void Main( string [] args)
{
Calculator num1 = new Calculator(200);
Calculator num2 = new Calculator(40);
Calculator num3 = new Calculator();
num3 = num1 + num2;
num1.display();
num2.display();
num3.display();
}
}
}
|
Output :
200
40
240
Benefits of Operator Overloading :
- Operator Overloading provides additional capabilities to C# operators when they are applied to user-defined data types.
- Operators may be considered as functions internal to the compiler.
Similar Reads
C# Method Overloading
Method overloading is an important feature of Object-Oriented programming and refers to the ability to redefine a method in more than one form. A user can implement method overloading by defining two or more functions in a class sharing the same name. C# can distinguish the methods with different me
5 min read
C# | String Operators
The string is an array of characters. The String class represents the text as a series of Unicode characters and it is defined in the .NET base class library. The main use of the String class is to provide the properties, operators and methods so that it becomes easy to work with strings.There are t
4 min read
C# Operators
In C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result. Types of Opera
8 min read
C# Program to Overload Unary Increment (++) and Decrement (--) Operators
In C#, overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement method overloading by defining two or more methods in a class sharing the same name but with different method signatures. So in this article, we wil
3 min read
C# | Bitwise OR operation between the elements of BitArray
The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Or(BitArray) method is used to perform the bitwise
3 min read
C# | Bitwise exclusive OR operation between the elements of BitArray
The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Xor(BitArray) method is used to perform the bitwise
3 min read
C# | Bitwise AND between the elements of BitArray
The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.And(BitArray) method is used to perform the bitwise
3 min read
Bitwise Complement Operator (~ tilde)
Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in Java The bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for t
3 min read
How to Take Operator as Input in C?
In C, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We may need to take operator as an input in some cases. In this article, we will learn how to take an operator as input in C. Get Input Operator in CTo take an operator as input in C, we
2 min read
C Program to Make a Simple Calculator
A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C. Example Input: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is additio
3 min read