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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...