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

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()
        {
        
        }
    }
}

Leave A Reply

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

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...