Compiler Error CS0019

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

A binary operator is operating on 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 it 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).

Example

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

static void Test()
        {
            bool result = true;
            if (result > 0) //CS0019
            {
                // Do something.
            }           

            int i = 1;
            if (i == true) //CS0019
            {
                //Do something...
            }
            
            string s = "Just try to subtract me.";
            float f = 100 - s; // CS0019
        }

In this example, conditional logic must be specified outside the ConditionalAttribute. You can only pass 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")]
   public void TestMethod2() {}
}

See Also

Reference

Operators (C# Programming Guide)

Change History

Date

History

Reason

July 2008

Added text re == operator and structs.

Content bug fix.

August 2008

Added new example.

Content bug fix.