Introduction to overloading operators in C#
Tags: csharp, operators, dotnet, learning
Published: 2019-04-04 15:07:31

---
title: Introduction to overloading operators in C#
published: true
description: The first entry in a series of posts about how you can modify the behavior of some of the operators available in C# such as addition (+), subtraction (-), among others.
tags: csharp, operators, dotnet, learning
---

This is the first entry in a series of posts about how you can modify the behavior of some of the operators available in C# such as addition (+), subtraction (-), among others.

## **Overloadable operators**
You can modify most unary and binary operators and other operators can be "modified" in other ways, and some of them can't be modified at all.

### Unary operators
Unary operators are those that only apply to one operand.
You can overload these unary operators:
* Addition (+)
* Subtraction (-)
* Logical negation (!)
* Increment and decrement (++ and --)
* Bitwise complement (~)
* true and false

### Binary operators
Binary operators are those that are applied to two operands.
You can overload these binary operators:
* Addition (+)
* Subtraction (-)
* Product (*)
* Division (/)
* Remainder (%)
* Logical AND (&)
* Logical OR (|)
* Logical XOR (^)
* Left and right shift (<< and >>)

### Comparison operators
Comparison operators are those that evualate an expression and return true or false based on whether the condition is met or not. You can overload these comparison operators:
* Equality (==)
* Inequality (!=)
* Less than (<)
* Greater than (>)
* Less than or equal (<=)
* Greater than or equal (>=)

Note that if you overload one of these operators, you also **must overload** the opposite operator. For example, if you overload the equality operator (==), you also need to overload the inequality operator (!=).

## **Non-explicitly overloadable operators**
Some operators cannot be explicitly overloaded, but you can use an alternative method that modifies the behavior when using that operator. Operators that cannot be explicitly overloaded are:
* Conditional logical operators (&& and ||), but under the hood they use the & and | operators which can be overloaded
* Array indexing operator [], but you can define [indexers](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/index) on an object
* Cast operator (T)x, but you can overload the [implicit](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit) and [explicit](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/explicit) operators
* Compound assignment operators (+=, -= and so on) but they are evaluated using the unary operators, which you can overload

## **Non-overloadable operators**
These are operators that are used by the compiler and their behavior cannot be modified in any way. They are the following:
* Assigment operator (=)
* Member access operator (.)
* Conditional (or ternary) operator (?:)
* Null-coalescing operator (??)
* [Dereference operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/dereference-operator) (->), used in unsafe (unmanaged) contexts
* Lambda operator (=>)
* Invocation operator ()
* *as* and *is* operators
* [checked](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked) and [unchecked](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unchecked) operators
* *default*, *delegate* and *new* operators
* *[sizeof](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sizeof)* and *[typeof](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/typeof)* operators


# Conclusion
This was an introductory entry so you can know which operators can and cannot be overloaded in the C# language. Stay tuned for the next entry, which will cover unary operators.