C# "CS0019: Operator cannot be applied to operands" in CI
CS0019 fires when an operator is used between two types it is not defined for - for example == between a struct and a class, or arithmetic on a string. It is a type-mismatch error, deterministic from the source.
What this error means
Build fails with CS0019 naming the operator and the two operand types. Reproduces on every build for the same code.
Calc.cs(14,20): error CS0019: Operator '+' cannot be applied to operands of type 'string'
and 'int'Common causes
Operands are incompatible types
The operator (==, +, <, etc.) is not defined between the two types as written, so the compiler rejects it.
A nullable/reference comparison was intended differently
Comparing a value type with null, or two reference types with no == overload, trips CS0019.
How to fix it
Convert operands to a compatible type
- Parse or cast one operand so both sides match the operator.
- For string building, convert numbers with
.ToString()or interpolation. - Rebuild.
var label = "count: " + count.ToString();
// or
var label = $"count: {count}";Use the right comparison
- Use
.Equalsor pattern matching where==is not defined. - For nullable value types, compare against
nullonly when the type is nullable. - Rebuild.
How to prevent it
- Let the compiler/analyzers flag mismatched operators before commit.
- Be explicit with conversions in mixed-type expressions.
- Prefer interpolation over string + non-string concatenation.