Skip to main content

How to Resolve Error "CS0019: Operator 'operator' cannot be applied to operands of type 'type1' and 'type2'" in C#

The Compiler Error CS0019 is a Type Mismatch error regarding operations. The message reads: "Operator 'operator' cannot be applied to operands of type 'type1' and 'type2'".

C# is a strongly typed language. This means the compiler strictly enforces rules about how data types interact. You cannot subtract an int from a string, nor can you use a logical AND (&&) on two integers (unlike C++ or JavaScript). This error occurs when you attempt to use a mathematical, logical, or comparison operator on types that do not support that specific interaction.

This guide demonstrates the common causes of this mismatch and how to resolve them.

Understanding the Error

Operators in C# (like +, -, ==, >, &&) are actually methods defined for specific types.

  • int has a method for +.
  • string has a method for + (concatenation).
  • string does not have a method for - (subtraction).

If you try to write "Hello" - "o", the compiler looks for a subtraction method taking two strings. It fails to find one and raises CS0019.

Scenario 1: Logical Operators on Integers (C++ Habits)

In languages like C++ or JavaScript, numbers are often treated as booleans (where 0 is false and anything else is true). In C#, logical operators (&&, ||) only work on booleans.

Problem

Trying to check if two numbers are "truthy" (non-zero) using logical AND.

int a = 10;
int b = 5;

// ⛔️ Error CS0019: Operator '&&' cannot be applied to operands of type 'int' and 'int'
if (a && b)
{
Console.WriteLine("Both are non-zero");
}

Solution

You must explicitly compare the numbers to turn them into booleans.

int a = 10;
int b = 5;

// ✅ Correct: Convert the logic to explicit boolean comparisons
if (a != 0 && b != 0)
{
Console.WriteLine("Both are non-zero");
}
note

If you intended to perform Bitwise operations (manipulating bits), use the single & or | operators. (a & b) is valid for integers, but it returns an int, not a bool.

Scenario 2: Comparing Incompatible Types

You cannot directly compare two completely different types, such as checking if a number equals a string representation of that number, without conversion.

Mistake

int age = 25;
string input = "25";

// ⛔️ Error CS0019: Operator '==' cannot be applied to operands of type 'int' and 'string'
if (age == input)
{
// ...
}

Solution

Convert one side to match the other.

// ✅ Correct: Parse the string to an int
if (age == int.Parse(input)) { }

// ✅ Correct: Or convert the int to a string
if (age.ToString() == input) { }

Scenario 3: Invalid Arithmetic on Strings

While C# allows you to add (+) strings (concatenation), it does not support other arithmetic like subtraction, multiplication, or division on strings.

Mistake

Trying to "remove" a word from a sentence using subtraction.

string phrase = "Hello World";

// ⛔️ Error CS0019: Operator '-' cannot be applied to operands of type 'string' and 'string'
string result = phrase - "World";

Solution

Use the string manipulation methods provided by the .NET framework.

string phrase = "Hello World";

// ✅ Correct: Use the Replace method
string result = phrase.Replace("World", "").Trim();
// Output: "Hello"

Scenario 4: Nullable Boolean Logic

This is a subtle error. In C#, a bool? (Nullable Boolean) can have three values: true, false, and null. The standard logical operators && and || are defined for bool, not bool? (though C# lifts them in specific ways, mixing them can cause issues).

Mistake

Trying to apply logic directly to a nullable without handling the null case explicitly in a context that expects strict boolean logic.

bool? isAvailable = null;

// ⛔️ Error CS0019 (in some contexts): Operator '||' cannot be applied
// specifically if mixing types incorrectly or in older C# versions.
// More commonly, this is about safety.
// A clearer example is equality:
if (isAvailable == 1) // Error CS0019: bool? vs int
{
}
note

Modern C# handles bool? & bool? via SQL-like 3-valued logic, but trying to use ! (NOT) on a null value or comparing incompatible nullable types triggers this.

Solution

Use the Null Coalescing Operator (??) or GetValueOrDefault().

bool? isAvailable = null;

// ✅ Correct: Treat 'null' as 'false' explicitly
if (isAvailable ?? false)
{
// Only runs if isAvailable is NOT null AND is true
}

Scenario 5: Operator Overloading for Custom Classes

If you create your own class (e.g., a ComplexNumber or Vector class), the compiler doesn't automatically know how to add or multiply them.

Mistake

public class Vector { public int X; public int Y; }

var v1 = new Vector { X = 1, Y = 1 };
var v2 = new Vector { X = 2, Y = 2 };

// ⛔️ Error CS0019: Operator '+' cannot be applied to operands of type 'Vector' and 'Vector'
var v3 = v1 + v2;

Solution

You must explicitly define (overload) the operator in your class.

public class Vector
{
public int X;
public int Y;

// ✅ Correct: Define how the '+' operator works for this class
public static Vector operator +(Vector a, Vector b)
{
return new Vector { X = a.X + b.X, Y = a.Y + b.Y };
}
}

// Now v1 + v2 works perfectly.

Conclusion

CS0019 is the compiler's way of saying "Apples cannot be multiplied by Oranges".

To resolve it:

  1. Check Data Types: Hover over your variables to ensure they are what you think they are.
  2. Convert Types: Use .ToString(), int.Parse(), or casts (int) to align the operand types.
  3. Use Explicit Logic: Don't rely on "truthy" integers; compare them to 0 or null explicitly.
  4. Overload Operators: If using custom classes, write the code to teach the compiler how to handle the math.