Advertisement
Intermediate C#, Indexers & Operator Overloading

Which operators can be overloaded in C#, and which ones (like && and ||) generally CANNOT be overloaded directly?

Short answer

You can overload most unary and binary operators (+, -, *, /, ==, !=, , etc.), true/false, and implicit/explicit conversion operators — but && and || specifically CANNOT be overloaded directly (they're derived automatically from overloading & and | plus true/false operators)…

Save C# mock interview
Advertisement
AModel answer
You can overload most unary and binary operators (+, -, *, /, ==, !=, <, >, etc.), true/false, and implicit/explicit conversion operators — but && and || specifically CANNOT be overloaded directly (they're derived automatically from overloading & and | plus true/false operators), and assignment operators like = can never be overloaded at all.
public static Vector operator +(Vector a, Vector b) => new(a.X + b.X, a.Y + b.Y); // OK
public static Vector operator *(Vector v, double scalar) => new(v.X * scalar, v.Y * scalar); // OK
// public static Vector operator &&(Vector a, Vector b) { } // NOT directly overloadable
Real-world example
Deciding which operators genuinely make semantic sense to overload for a custom Vector, Money, or Complex number type.
Likely follow-ups
  • How would you make a custom type work correctly with '&&' indirectly, given it can't be overloaded directly?
Related topics