Operator overloading in C sharp

Before you venture into learning operator overloading in C#, it is a prerequisite that you must be acquainted with operators in C#. The same concept of overloading a function can be applied to operators. In programming, overloading operators gives us the ability to use various functions using the same operator.

Overloading operators gives C# operators additional capabilities when they are applied to user-defined data types. The programmer can make user-defined implementations of various operations where one or both of the operands are user-defined class.

In C# we can only overload predefined set of operators. It is quite hectic to perform operations on a user-defined data_type. You can overload operators with user-defined data types according to your requirements. To do this, you should define a function to the operator. An operator keyword is used to declare the function of the operator:

Syntax

Access specifier className operator operator_symbol (parameters)

{

//Body

}

Point to note: Operator overloading in C# provides special meaning to ideal operators.

Here is a table that describes the overloading ability of various operators available in C#

Operators

Description

-, +, –, ++, !,

These are unary operators. In C-sharp, they can be overloaded and take one operand.

*, +, -, /, %

These are binary operators. They take two operands and can be overloaded

!=, ==, =

These are comparison operators. They can be overloaded.

&&,||

These are known as conditional logical operators. They cannot be overloaded directly.

-+, +=, *=, %=, /=, =

They are known as assignment operators. They cannot be overloaded.

How do we overload unary operators in C sharp?

In C-sharp the return type for unary operators:

  • !, + and dot – Can be of any type except void
  • – and++ – Must be the type of ‘Type’
  • True or false operators – Must be of bool type

Note: The true and false operators can only be overloaded as pairs. If a class declares one of these operators without declaring the other, a compilation error will arise.

The following syntax denotes the use of unary operator:

Operator (object);

//In this syntax, the operator is a symbol that denotes a unary operator.

Operator a;

  • Simple prefix unary operators

This type of unary operator is defined by the function that takes one parameter. The ‘object 1’ will be interpreted by the compiler in the following manner.

Operator – (object1);

We can associate the -() with the class by creating a member function. This will create an association of the function with the class.

  • Pre and post-increment and decrement operators

In c sharp:

A decrement operator decrements the value of the current value by one while an increment value increments the value of an operand by one. Both operands return the results. The prefix and post fix notations must be static, unary and public.

How do we overload binary operators in C sharp?

These types of operators work with two operands. Binary operators examples include:

  • Arithmetic operators – They include *, +, – , %, and /
  • Relation operators – They include <, <=, >, >+, ==, and !=
  • Arithmetic assignment operators –  Examples are  %=, +=, -+, /+, and *=

We overload binary operators the same way we overload unary operators. The only difference is that a binary operator requires an additional parameter.

The syntax:

Operator operator (object 1, object 2);

In this syntax, the second “operator” is a symbol that denotes a binary operator.

Operator + (a, b);

How do we overload equality operators in C sharp?

All user-defined classes inherit from syste.object by default. They inherit the system.object.Equals () method. A reference-based comparison is provided by the default implementation of Equals (). This method can be overridden inside the user-defined class to provide a value-based comparison.

Operator overloading and inheritance

The overloaded operators in C# are declared as static. However, they are inherited to the subclasses (derived classes). An operator declaration requires structure or class in which it is to be declared. It is impossible for an operator declared in a derived class to hide an operator declared in the parent class. For this reason, a new modifier is never required and permitted in an operator declaration

What are the benefits of operator overloading in C#

  • As we mentioned earlier, operator overloading adds additional capabilities to the operators in C# when applied to user-defined data types
  • Operators can be considered as functions internal to the computer

Avail of our C sharp programming assignment help today for instant help with your assignment.