Compiler Error CS0019

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Operator 'operator' cannot be applied to operands of type 'type' and 'type'

A binary operator is applied to data types that do not support it. For example, you cannot use the || operator on strings, you cannot use + , - , < , or > operators on bool variables, and you cannot use the == operator with a struct type unless the type explicitly overloads that operator.

If you encounter this error with a class type, it is because the class does not overload the operator. For more information, see Overloadable Operators.

In the following example, CS0019 is generated in two places because bool in C# is not convertible to int. CS0019 also is generated when the subtraction operator is applied to a string. The addition operator (+) can be used with string operands because that operator is overloaded by the String class to perform string concatenation.

static void Main()  
{  
    bool result = true;  
    if (result > 0) //CS0019  
    {  
        // Do something.  
    }  
  
    int i = 1;  
    // You cannot compare an integer and a boolean value.  
    if (i == true) //CS0019  
    {  
        //Do something...  
    }  
  
    // The following use of == causes no error. It is the comparison of  
    // an integer and a boolean value that causes the error in the   
    // previous if statement.  
    if (result == true)  
    {  
        //Do something...  
    }  
  
    string s = "Just try to subtract me.";  
    float f = 100 - s; // CS0019  
}  
  

In the following example, conditional logic must be specified outside the ConditionalAttribute. You can pass only one predefined symbol to the ConditionalAttribute.

The following sample generates CS0019.

// CS0019_a.cs  
// compile with: /target:library  
using System.Diagnostics;  
public class MyClass  
{  
   [ConditionalAttribute("DEBUG" || "TRACE")]   // CS0019  
   public void TestMethod() {}  
  
   // OK  
   [ConditionalAttribute("DEBUG"), ConditionalAttribute("TRACE")]  
   public void TestMethod2() {}  
}  
  

Operators
Implicit Numeric Conversions Table

Show: