Skip to content
Tutorials Center
My learning

Topics

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.

C#
bool isReady = true;
bool canEdit = isReady && !isLocked;

Try it

C# Operators and short-circuiting

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.

C# Parsing, conversion and bool?

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

C#
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 & false is false and null | true is true; every other combination with null gives null. Comparing with == null is 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. Write if (isReady) or if (!isReady). For bool?, write if (maybe == true) or maybe ?? false.
  • Naming. Name booleans like questions: isEmpty, hasItems, canRetry.
  • Flags. Many bool parameters in one method signal a design problem; replace them with an enum or an options object.
  • Memory. An array of bool uses one byte per element; use BitArray or BitVector32 for dense bit sets.
  • Pattern matching. x is true, x is not null and x is { Length: > 0 } are all boolean expressions.