Peristiwa
17 Mac, 9 PTG - 21 Mac, 10 PG
Sertai siri perjumpaan untuk membina penyelesaian AI berskala berdasarkan kes penggunaan dunia sebenar dengan rakan pembangun dan pakar.
Daftar sekarangPelayar ini tidak lagi disokong.
Naik taraf kepada Microsoft Edge untuk memanfaatkan ciri, kemas kini keselamatan dan sokongan teknikal yang terkini.
The true
operator returns the bool value true
to indicate that its operand is definitely true, while the false
operator returns the bool
value true
to indicate that its operand is definitely false.
Note that a type implementing both true
and false
operators has to follow these semantics:
true
. Operator true
returns true
if the object is true
. The answer is "Yes, this object is true".false
. Operator false
returns true
if the object is false
. The answer is "Yes, this object is false"The true
and false
operators aren't 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 these two operators, it must also define the other operator.
Petua
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 Boolean type). C# provides the &
and |
operators that support the three-valued logic with the bool?
operands. For more information, see the Nullable Boolean logical operators section of the Boolean logical operators article.
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.
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.
The following example presents the type that defines both true
and false
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 the GetFuelLaunchStatus
method returns LaunchStatus.Red
, the right-hand operand of the &&
operator isn't evaluated. That is because LaunchStatus.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!
.NET maklum balas
.NET ialah projek sumber terbuka. Pilih pautan untuk memberikan maklum balas:
Peristiwa
17 Mac, 9 PTG - 21 Mac, 10 PG
Sertai siri perjumpaan untuk membina penyelesaian AI berskala berdasarkan kes penggunaan dunia sebenar dengan rakan pembangun dan pakar.
Daftar sekarang