true and false operators (C# Reference)

The true operator returns the bool value true to indicate that an operand is definitely true. The false operator returns the bool value true to indicate that an operand is definitely false. The true and false operators are not guaranteed to complement each other. That is, both the true and false operator might return the bool value false for the same operand. If a type defines one of the two operators, it must also define another operator.

If a type with the defined true and false operators overloads the logical OR operator | or the logical AND operator & in a certain way, the conditional logical OR operator || or conditional logical AND operator &&, respectively, can be evaluated for the operands of that type. For more information, see the User-defined conditional logical operators section of the C# language specification.

A type with the defined true operator can be the type of a result of a controlling conditional expression in the if, do, while, and for statements and in the conditional operator ?:. For more information, see the Boolean expressions section of the C# language specification.

Tip

Use the bool? type, if you need to support the three-valued logic, for example, when you work with databases that support a three-valued logical type. For more information, see The bool? type section of the Using nullable types article.

The following example presents the type that defines both true and false operators. Moreover, it overloads the logical AND operator & in such a way that the operator && also can be evaluated for the operands of that type.

using System;

public struct LaunchStatus
{
    public static readonly LaunchStatus Green = new LaunchStatus(0);
    public static readonly LaunchStatus Yellow = new LaunchStatus(1);
    public static readonly LaunchStatus Red = new LaunchStatus(2);

    private int status;

    private LaunchStatus(int status)
    {
        this.status = status;
    }

    public static bool operator true(LaunchStatus x) => x == Green || x == Yellow;
    public static bool operator false(LaunchStatus x) => x == Red;

    public static LaunchStatus operator &(LaunchStatus x, LaunchStatus y)
    {
        if (x == Red || y == Red || (x == Yellow && y == Yellow))
        {
            return Red;
        }

        if (x == Yellow || y == Yellow)
        {
            return Yellow;
        }

        return Green;
    }

    public static bool operator ==(LaunchStatus x, LaunchStatus y) => x.status == y.status;
    public static bool operator !=(LaunchStatus x, LaunchStatus y) => !(x == y);

    public override bool Equals(object obj) => obj is LaunchStatus other && this == other;
    public override int GetHashCode() => status;
}

public class LaunchStatusTest
{
    public static void Main()
    {
        LaunchStatus okToLaunch = GetFuelLaunchStatus() && GetNavigationLaunchStatus();
        Console.WriteLine(okToLaunch ? "Ready to go!" : "Wait!");
    }

    static LaunchStatus GetFuelLaunchStatus()
    {
        Console.WriteLine("Getting fuel launch status...");
        return LaunchStatus.Red;
    }

    static LaunchStatus GetNavigationLaunchStatus()
    {
        Console.WriteLine("Getting navigation launch status...");
        return LaunchStatus.Yellow;
    }
}

Notice the short-circuiting behavior of the && operator. When the GetFuelLaunchStatus method returns LaunchStatus.Red, the second operand of the && operator is not evaluated. That is because LaunchStatus.Red is definitely false. Then the result of the logical AND doesn't depend on the value of the second operand. The output of the example is as follows:

Getting fuel launch status...
Wait!

See also