Introduction to overloading operators in C#

jefrypozo

Jefry Pozo

Posted on April 4, 2019

Introduction to overloading operators in C#

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 on an object
  • Cast operator (T)x, but you can overload the implicit and 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 (->), used in unsafe (unmanaged) contexts
  • Lambda operator (=>)
  • Invocation operator ()
  • as and is operators
  • checked and unchecked operators
  • default, delegate and new operators
  • sizeof and 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.

💖 💪 🙅 🚩
jefrypozo
Jefry Pozo

Posted on April 4, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related