C# Error CS0019 – Operator ‘{0}’ cannot be applied to operands of type ‘{1}’ and ‘{2}’

C# Compiler Error Message

Operator ‘{0}’ cannot be applied to operands of type ‘{1}’ and ‘{2}’

Reason for the Error

You would usually get this error when you are using an operator that doesn’t support a specific data type. Below are some of the cases when you will receive this error.

  • When you use bool and think, it works as integer.
public class Hello {
    public static void Main() {
            bool input = true;
            if (input > 0) // This Line results in cs0019 error.
            {
                // Do something.
            }
    }
}

Error CS0019 Operator ‘>’ cannot be applied to operands of type ‘bool’ and ‘int’ ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 6 Active

  • When you compare an int with boolean
namespace ClassLibrary
{
    public class DeveloperPublish
    {
        public static void Main()
        {
            int input = 1;
            if (input == true)
            {
            }
        }
    }
}

You will receive the below error.

Error CS0019 Operator ‘==’ cannot be applied to operands of type ‘int’ and ‘bool’ ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 8 Active

Some of the Other common scenarios that would result with this error includes

  • When you use || operator on strings
  • When you use +,- on boolean.
  • When you use == with structs

Solution

To fix the error, ensure that you revisit the logic and ensure that the right operator is used for the operands in your .NET application.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...