Operators permit a class to define expression operators that can be applied to instances of the class. Operators are declared using operator-declarations:
There are three categories of operators: Unary operators (§10.9.1), binary operators (§10.9.2), and conversion operators (§10.9.3).
The following rules apply to all operator declarations:
public
and a static
modifier, and is not permitted to include any other modifiers.ref
or out
parameters.Each operator category imposes additional restrictions, as described in the following sections.
Like other members, operators declared in a base class are inherited by derived classes. Because operator declarations always require the class or struct in which the operator is declared to participate in the signature of the operator, it is not possible for an operator declared in a derived class to hide an operator declared in a base class. Thus, the new
modifier is never required, and therefore never permitted, in an operator declaration.
For all operators, the operator declaration includes a block which specifies the statements to execute when the operator is invoked. The block of an operator must conform to the rules for value-returning methods described in §10.5.7.
Additional information on unary and binary operators can be found in §7.2.
Additional information on conversion operators can be found in §6.4.