Skip to content
Tutorials Center
My learning

Topics

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.

C#
public static Vec operator +(Vec a, Vec b) => new(a.X + b.X, a.Y + b.Y);

Try it

C# Arithmetic and comparison operators

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.

C# Equality pairs and a user-defined true/false

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.

C# C# 14 compound assignment on a class

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

C#
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, and x++ from operator ++. Since C# 14 you may declare the compound form yourself as an instance method returning void, which lets a type update itself in place without allocating a new value. If you declare it, a += b calls it.
  • Overload == and != together, and also override Equals and GetHashCode consistently. For simple data types, a record or record struct generates all of this for you.
  • Inside an overloaded ==, compare with is null or ReferenceEquals, never a == null, which recurses forever.
  • Shift operators used to require an int right operand; since C# 11 the right operand can be any type.
  • C# 11 added checked variants (public static T operator checked +(T a, T b)) that run in checked contexts, 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.