Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Compiler Error CS0019
Collapse the table of content
Expand the table of content

Compiler Error CS0019

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 (C# Programming Guide).

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() {}
}
Show:
© 2015 Microsoft