Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0215 – The return type of operator True or False must be bool

C# Compiler Error

CS0215 – The return type of operator True or False must be bool.

Reason for the Error

You will receive this error in your C# program when you try to use operator overloading or user defined operators and donot return a boolean type.

For example, try to compile the below code snippet.

namespace DeveloperPubNamespace
{   
    class Program
    {
        public static int operator true(Program obj)  
        {
            return true;
        }
        public static int operator false(Program obj)
        {
            return false;
        }
        static void Main()
        {
        
        }
    }
}

This program will result with the error code CS0215 because the user defined true and false operator returns int type instead of boolean.

Error CS0215 The return type of operator True or False must be bool ConsoleApp3 C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 5 Active

C# Error CS0215 – The return type of operator True or False must be bool

Solution

You can fix this error in your C# program by ensuring that you are returning bool type as shown below.

namespace DeveloperPubNamespace
{   
    class Program
    {
        public static bool operator true(Program obj)  
        {
            return true;
        }
        public static bool operator false(Program obj)
        {
            return false;
        }
        static void Main()
        {
        
        }
    }
}