Home
Map
true and falseUnderstand the true and false keywords. See example programs that use if-statements and bools.
C#
This page was last reviewed on May 10, 2023.
True, false. In C# true and false are boolean literals. They are values that mean yes and no. They can be stored in variables of type bool.
In the C# language, true and false are lowercase reserved keywords. We cannot specify true and false with integers directly—0 and 1 are not compatible.
bool
int, uint
True. We look at a program that uses the true keyword. We can use true in if-statements, while-statements, expressions, assignments and negations.
if
while
Tip In the C# language, the if-statement requires that its expression be evaluated in a bool context.
And This means you may sometimes need to explicitly specify the comparison—you cannot directly test integers.
using System; // Reachable. if (true) { Console.WriteLine(1); } // Expressions can evaluate to true or false. if ((1 == 1) == true) { Console.WriteLine(2); } // While true loop. while (true) { Console.WriteLine(3); break; } // Use boolean to store true. bool value = true; // You can compare bool variables to true. if (value == true) { Console.WriteLine(4); } if (value) { Console.WriteLine(5); }
1 2 3 4 5
False. The false keyword is a constant boolean literal, meaning it is a value that can be stored in a variable. It can also be evaluated in an if-expression.
Start In the first if-statement, we see that when an expression evaluates to false, the code inside the if-block is not reached.
Info You can set a bool variable to false. You can invert the value of a bool variable with the exclamation operator.
Finally The expression != false is equal to == true. This can be entirely omitted.
Tip It is usually clearer to express conditions in terms of truth rather than testing against false, but sometimes false may be clearer.
using System; // Unreachable. if (false) { Console.WriteLine(); // Not executed. } // Use boolean to store true and false. bool value = true; Console.WriteLine(value); // True value = false; Console.WriteLine(value); // False value = !value; Console.WriteLine(value); // True // Expressions can be stored in variables. bool result = (1 == int.Parse("1")) != false; Console.WriteLine(result); // True
True False True True
TrueString, FalseString. Truth has a string representation. The bool.TrueString and bool.FalseString fields provide the string literals "True" and "False".
String Literal
Info These two fields are implemented as public static readonly fields. They return "True" and "False" in this program.
readonly
Note In other locales, it is possible they will return other strings. We compare these fields and use them just like normal strings.
using System; // Write values of these properties. Console.WriteLine(bool.TrueString); Console.WriteLine(bool.FalseString); Console.WriteLine(); Console.WriteLine(bool.TrueString == "True"); Console.WriteLine(bool.FalseString == "False");
True False True True
Review. Most parts of true and false are clear to understand. You can change the value of a bool that is set from assignment to an expression by applying a == true or != true at the end.
Detail The true keyword is very frequently used. There are some subtleties to its use, particularly in assignments to expressions.
Tip If-statements and while-statements require a bool processing context, which mandates the usage of true—or false.
Bool. True and false are stored in bool variables. Bools are not directly convertible to value types such as int. Instead, a special conversion method must be used.
Convert Bool, Int
bool.Parse
True and false are commonly used values in C# programs. They can be stored in a variable of type bool. These 2 keywords are boolean literals.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 10, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.