C# Reference ยท Keywords and Statements
The operator keyword
Reference for the C# operator keyword: overload arithmetic, comparison and equality operators for your types, with paired-operator rules and C# 14 compound assignment.
The operator keyword declares a user-defined operator. With it, a class or struct can give meaning to symbols like +, == or < so that a + b reads naturally for vectors, money, fractions or any other value-like type.
An operator is a public static method with a special name. At least one parameter must be the type that declares it, which stops you from changing what 1 + 2 means. Use overloading only where the meaning is obvious; otherwise a normal method name is clearer.
public static Vec operator +(Vec a, Vec b) => new(a.X + b.X, a.Y + b.Y);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static ReturnType operator op(Type operand) // unary public static ReturnType operator op(Type left, Type right) // binary public static implicit operator Target(Source s) // conversions
| Category | Operators you can overload |
|---|---|
| Unary | + - ! ~ ++ -- true false |
| Binary | + - * / % & | ^ << >> >>> |
| Comparison | == != < > <= >= (overload in matching pairs) |
| Conversion | implicit and explicit, see implicit and explicit |
| C# 14 compound | instance void operator +=(T other) and the other compound forms |
Cannot be overloaded: =, ., ?., ?:, ??, &&, ||, ->, new, is, as, typeof, sizeof, checked/unchecked blocks, and indexing [] (use an indexer instead). && and || work automatically if you overload &/| together with true/false.
Errors: a declaration that breaks the rules is rejected: a missing pair gives CS0216 (== without !=, < without >), a non-public static operator gives CS0558, and a signature without the enclosing type gives CS0563.
Usage notes
- Compound assignment
+=is built from+automatically, andx++fromoperator ++. Since C# 14 you may declare the compound form yourself as an instance method returningvoid, which lets a type update itself in place without allocating a new value. If you declare it,a += bcalls it. - Overload
==and!=together, and also overrideEqualsandGetHashCodeconsistently. For simple data types, arecordorrecord structgenerates all of this for you. - Inside an overloaded
==, compare withis nullorReferenceEquals, nevera == null, which recurses forever. - Shift operators used to require an
intright operand; since C# 11 the right operand can be any type. - C# 11 added
checkedvariants (public static T operator checked +(T a, T b)) that run incheckedcontexts, and static abstract members in interfaces make operators usable in generic math (INumber<T>). - Keep operators cheap, side-effect free and symmetrical. Prefer immutable value types; operators that mutate their arguments are surprising.