CS0019
C# Programming Language
Severity: MinorWhat Does This Error Mean?
CS0019 means you used a mathematical, comparison, or logical operator on two values whose types do not support that operation. For example: trying to add two custom objects with +, or comparing a string to a bool with >. C# only allows operators on types where those operations make sense. The fix is usually a conversion, a method call, or overloading the operator on your custom type.
Affected Models
- .NET Framework
- .NET Core
- .NET 5+
- Visual Studio
- Visual Studio Code
- Rider
Common Causes
- Trying to use arithmetic operators (+, -, *, /) on custom objects that have not defined those operators
- Comparing two incompatible types with == where no comparison is defined
- Using bitwise operators on non-integer types like float or double
- Applying the + operator to types where it is not meaningful — like adding a DateTime to a string without conversion
- Using relational operators (<, >, <=, >=) on types that do not implement comparison
How to Fix It
-
Read the error: 'Operator X cannot be applied to operands of type Y and Z'. Identify which operator you used and on which types.
Example: 'Operator + cannot be applied to operands of type DateTime and DateTime' — you cannot add two DateTimes directly. Use TimeSpan instead.
-
Convert one or both values to a compatible type before applying the operator.
Example: if you have a DateTime and want to add time, use myDate.AddDays(5) or myDate + TimeSpan.FromDays(5) instead of direct addition.
-
For comparing custom objects, implement IComparable<T> on the class or override the Equals() method and == operator.
If you control the class, adding 'public static bool operator ==(MyClass a, MyClass b)' defines what == means for your type.
-
For string concatenation with non-string types, convert to string first using .ToString() or use string interpolation.
Example: instead of myString + myDateTime, use myString + myDateTime.ToString() or $'{myString}{myDateTime}'
-
Check if there is a method that does what you were trying to do with the operator. Many types have methods for common operations that operators cannot express.
Example: TimeSpan.Subtract() for subtracting time intervals, String.Compare() for comparing strings, Math.Max() for finding the larger of two values.
When to Call a Professional
CS0019 is a compile-time error you can fix yourself. The error message names the operator and both operand types clearly. For custom types, you may need to define operator overloads. For built-in type mismatches, conversion methods or a different approach will solve it.
Frequently Asked Questions
Can I make my own class support operators like + or ==?
Yes — this is called operator overloading. You define a special static method in your class using the 'operator' keyword. Example: 'public static Vector operator +(Vector a, Vector b) { return new Vector(a.X + b.X, a.Y + b.Y); }' Once defined, you can write 'vector1 + vector2' just like with numbers. This is used in classes like DateTime, TimeSpan, and most math-related types.
Why can I not subtract two DateTime values in C#?
Actually, you can — but the result is a TimeSpan, not a DateTime. Write 'TimeSpan difference = dateTime2 - dateTime1;' and it works. You cannot add two DateTimes together though — that would be meaningless (what would two dates added together represent?). To add time to a date, use the AddDays(), AddHours(), or AddMinutes() methods.
Why does == sometimes not work as expected on custom objects?
By default, == on custom objects checks if the two variables point to the exact same object in memory — not if they have equal contents. This is called reference equality. If you want == to compare by content (value equality), you must override the Equals() method and the == operator in your class. For comparing values, many developers prefer using the Equals() method explicitly to make the intent clear.