Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
You can overload an operator to make it support operands of certain types. For more information, see Operator overloading.
Example 1
In the following example, CS0019 is generated in three places because bool in C# is not convertible to int. CS0019 is also 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...
}
string s = "Just try to subtract me.";
float f = 100 - s; // CS0019
}
Example 2
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() {}
}