How to Resolve Error "CS1020: Overloadable binary operator expected" in C#
The Compiler Error CS1020 is a syntax error related to Operator Overloading. The message reads: "Overloadable binary operator expected".
In C#, operators are categorized by the number of operands they accept. Binary Operators accept exactly two parameters (e.g., a + b). This error occurs when you define an operator overload method with two parameters, but the symbol you provided (e.g., !, ++, or +=) is not recognized by C# as a valid binary operator.
This guide explains which operators are valid for binary overloading and how to fix syntax mismatches.
Understanding Binary Operators
When you declare an operator with two parameters:
public static ReturnType operator Symbol(TypeA a, TypeB b)
The Symbol must be one of the following overloadable binary operators:
- Arithmetic:
+,-,*,/,% - Bitwise:
&,|,^,<<,>> - Comparison:
==,!=,<,>,<=,>=
Any other symbol (like !, ~, or +=) is invalid in a two-parameter signature.
Scenario 1: Using Unary Symbols with Two Parameters
This error often occurs when a developer tries to apply logic involving two objects but uses a symbol reserved for single objects (Unary operators).
Example of error
Attempting to overload the Logical NOT (!) operator to take two arguments (perhaps thinking it means "Not Equal").
public class Toggle
{
public bool IsOn { get; set; }
// ⛔️ Error CS1020: Overloadable binary operator expected.
// The compiler sees 2 parameters ('a', 'b').
// It expects a binary symbol like '==', but found '!', which is unary.
public static bool operator !(Toggle a, Toggle b)
{
return a.IsOn != b.IsOn;
}
}
Solution: Use the Correct Symbol
If you want to compare two objects, use != (Inequality) or == (Equality). If you want to invert one object, remove the second parameter.
public class Toggle
{
public bool IsOn { get; set; }
// ✅ Correct: '!=' is a valid binary operator
public static bool operator !=(Toggle a, Toggle b)
{
return a.IsOn != b.IsOn;
}
// ✅ Correct: '==' must act as the pair to '!='
public static bool operator ==(Toggle a, Toggle b)
{
return a.IsOn == b.IsOn;
}
// Required overrides for ==/!=
public override bool Equals(object o) => true;
public override int GetHashCode() => 0;
}
Scenario 2: Attempting to Overload Compound Assignment (+=)
A very common mistake is trying to overload +=, -=, *=, etc. In C#, these are Compound Assignment Operators. You cannot overload them directly. Instead, you overload the underlying binary operator (e.g., +), and the compiler automatically generates the += behavior for you.
Example of error
Trying to define custom logic specifically for +=.
public class Score
{
public int Points { get; set; }
// ⛔️ Error CS1020: Overloadable binary operator expected.
// '+=' is not an overloadable operator.
public static Score operator +=(Score s, int p)
{
s.Points += p;
return s;
}
}
Solution: Overload the Binary Operator
Overload the standard addition (+) operator. The compiler will then allow s += p by translating it to s = s + p.
public class Score
{
public int Points { get; set; }
// ✅ Correct: Overload '+'.
// C# automatically enables '+=' based on this definition.
public static Score operator +(Score s, int p)
{
// Return a new object (Operators should generally be immutable)
return new Score { Points = s.Points + p };
}
}
public class Program
{
static void Main()
{
Score myScore = new Score { Points = 10 };
// This works because we overloaded '+'
myScore += 5;
}
}
Immutability: Operator overloads should ideally return a new instance rather than modifying the operands. This ensures a + b doesn't accidentally change a.
Conclusion
CS1020 indicates a mismatch between the parameter count (2) and the operator symbol.
- Check the Symbol: Is it
!,~,++,--? These are Unary. They only accept one parameter. - Check Compound Assignment: Is it
+=,-=? These cannot be overloaded. Overload+or-instead. - Check Logic: Ensure you are using valid binary symbols (
+,-,==,!=, etc.) when defining operators with two arguments.