Skip to content

Latest commit

 

History

History
64 lines (46 loc) · 7.42 KB

operator-overloading.md

File metadata and controls

64 lines (46 loc) · 7.42 KB
title description ms.date f1_keywords helpviewer_keywords
Operator overloading - Define unary, arithmetic, equality, and comparison operators.
Learn how to overload a C# operator and which C# operators are overloadable. In general, the unary, arithmetic, equality and comparison operators are overloadable.
11/28/2022
operator_CSharpKeyword
operator
operator keyword [C#]
operator overloading [C#]

Operator overloading - predefined unary, arithmetic, equality and comparison operators

A user-defined type can overload a predefined C# operator. That is, a type can provide the custom implementation of an operation in case one or both of the operands are of that type. The Overloadable operators section shows which C# operators can be overloaded.

Use the operator keyword to declare an operator. An operator declaration must satisfy the following rules:

  • It includes both a public and a static modifier.
  • A unary operator has one input parameter. A binary operator has two input parameters. In each case, at least one parameter must have type T or T? where T is the type that contains the operator declaration.

The following example defines a simplified structure to represent a rational number. The structure overloads some of the arithmetic operators:

[!code-csharpfraction example]

You could extend the preceding example by defining an implicit conversion from int to Fraction. Then, overloaded operators would support arguments of those two types. That is, it would become possible to add an integer to a fraction and obtain a fraction as a result.

You also use the operator keyword to define a custom type conversion. For more information, see User-defined conversion operators.

Overloadable operators

The following table shows the operators that can be overloaded:

Operators Notes
+x, -x, !x, ~x, ++, --, true, false The true and false operators must be overloaded together.
x + y, x - y, x * y, x / y, x % y,
x & y, x | y, x ^ y,
x << y, x >> y, x >>> y
x == y, x != y, x < y, x > y, x <= y, x >= y Must be overloaded in pairs as follows: == and !=, < and >, <= and >=.

Non overloadable operators

The following table shows the operators that can't be overloaded:

Operators Alternatives
x && y, x || y Overload both the true and false operators and the & or | operators. For more information, see User-defined conditional logical operators.
a[i], a?[i] Define an indexer.
(T)x Define custom type conversions that can be performed by a cast expression. For more information, see User-defined conversion operators.
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>= Overload the corresponding binary operator. For example, when you overload the binary + operator, += is implicitly overloaded.
^x, x = y, x.y, x?.y, c ? t : f, x ?? y, ??= y,
x..y, x->y, =>, f(x), as, await, checked, unchecked, default, delegate, is, nameof, new,
sizeof, stackalloc, switch, typeof, with
None.

C# language specification

For more information, see the following sections of the C# language specification:

See also