C# Reference ยท Types and Literals
C# bool type
Reference for the C# bool type: true and false, logical operators, short-circuiting, conversions, bool? three-valued logic, Parse and TryParse.
bool (System.Boolean) holds one of two values: true or false. It is the type of every comparison and the only type the condition of an if, while, for or ?: accepts. Unlike C and C++, C# never treats integers, pointers or null as truth values, so if (count) is a compile error and you write if (count != 0).
A bool takes one byte in memory, and its default value is false. Combine booleans with &&, ||, !, &, | and ^; the doubled forms short-circuit, which is what you want for guards such as x != null && x.Length > 0.
bool isReady = true; bool canEdit = isReady && !isLocked;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
bool value = true; bool? maybe = null; // Nullable<bool> if (value) { /* ... */ }
| Operator | Name | Notes |
|---|---|---|
!x |
logical negation | |
x && y |
conditional AND | y is evaluated only if x is true. |
x || y |
conditional OR | y is evaluated only if x is false. |
x & y, x | y |
logical AND, OR | Evaluate both operands. |
x ^ y |
logical XOR | true when exactly one side is true. |
==, != |
equality |
| Member | Description |
|---|---|
bool.Parse(string) |
Accepts "True" or "False" in any case, ignoring surrounding white space. Throws FormatException otherwise. |
bool.TryParse(string, out bool) |
Returns false instead of throwing. |
bool.TrueString, bool.FalseString |
The strings "True" and "False". |
Conversions. There are no implicit or explicit casts between bool and numeric types. Use Convert.ToBoolean and Convert.ToInt32, or write the comparison yourself.
Errors / undefined behavior. Nothing throws except parsing. The true/false operators can be overloaded for user types but are rarely needed.
Usage notes
bool?logic is three-valued.null & falseisfalseandnull | trueistrue; every other combination withnullgivesnull. Comparing with== nullis the safe test for "unknown".- Prefer
&&and||. The single-character forms are for when both sides must run (they have side effects) or for bitwise work on integers. - Avoid
== true. Writeif (isReady)orif (!isReady). Forbool?, writeif (maybe == true)ormaybe ?? false. - Naming. Name booleans like questions:
isEmpty,hasItems,canRetry. - Flags. Many
boolparameters in one method signal a design problem; replace them with an enum or an options object. - Memory. An array of
booluses one byte per element; useBitArrayorBitVector32for dense bit sets. - Pattern matching.
x is true,x is not nullandx is { Length: > 0 }are all boolean expressions.
Related
ifandelse,switch, Nullable value types,char- Lessons: Booleans and chars, Operators