Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

true and false operators - treat your objects as a Boolean value

  • 2024-02-23
Feedback

In this article

Thetrue operator returns thebool valuetrue to indicate that its operand is definitely true, while thefalse operator returns thebool valuetrue to indicate that its operand is definitely false.
Note that a type implementing bothtrue andfalse operators has to follow these semantics:

  • "Is this object true?" resolves to operatortrue. Operatortrue returnstrue if the object istrue. The answer is "Yes, this object is true".
  • "Is this object false?" resolves to operatorfalse. Operatorfalse returnstrue if the object isfalse. The answer is "Yes, this object is false"

Thetrue andfalse operators aren't guaranteed to complement each other. That is, both thetrue andfalse operator might return thebool valuefalse for the same operand. If a type defines one of these two operators, it must also define the other operator.

Tip

Use thebool? type, if you need to support the three-valued logic (for example, when you work with databases that support a three-valued Boolean type). C# provides the& and| operators that support the three-valued logic with thebool? operands. For more information, see theNullable Boolean logical operators section of theBoolean logical operators article.

Boolean expressions

A type with the definedtrue operator can be the type of a result of a controlling conditional expression in theif,do,while, andfor statements and in theconditional operator?:. For more information, see theBoolean expressions section of theC# language specification.

User-defined conditional logical operators

If a type with the definedtrue andfalse operatorsoverloads thelogical OR operator| or thelogical AND operator& in a certain way, theconditional logical OR operator|| orconditional logical AND operator&&, respectively, can be evaluated for the operands of that type. For more information, see theUser-defined conditional logical operators section of theC# language specification.

Example

The following example presents the type that defines bothtrue andfalse operators. The type also overloads the logical AND operator& in such a way that the&& operator also can be evaluated for the operands of that type.

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 theGetFuelLaunchStatus method returnsLaunchStatus.Red, the right-hand operand of the&& operator isn't evaluated. That is becauseLaunchStatus.Red is definitely false. Then the result of the logical AND doesn't depend on the value of the right-hand operand. The output of the example is as follows:

Getting fuel launch status...Wait!

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo