DEV Community

Jefry Pozo
Jefry Pozo

Posted on • Edited on

3 1

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.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️