C# Booleans

A Boolean is a value that can be only true or false. C# uses Booleans to represent yes-or-no facts such as whether a user is logged in, whether a number is valid, or whether a loop should keep running. Booleans matter because almost every decision in a program eventually becomes a Boolean expression.

Overview: How C# Booleans Work

The C# keyword bool is an alias for the .NET type System.Boolean. A bool variable stores one of two logical values: true or false. Unlike some languages, C# does not treat numbers, strings, objects, or collections as Booleans. The integer 1 is not true, 0 is not false, and an empty string cannot be used directly as a condition. This strictness prevents many accidental bugs.

Booleans are produced in two common ways. First, you can write Boolean literals directly: bool isActive = true;. Second, comparison expressions produce Boolean results. For example, age >= 18, name == "Maya", and total != 0 all evaluate to either true or false. Those results can be stored in variables, printed, returned from methods, or used in if, while, and for statements.

Logical operators combine Boolean expressions. && means both sides must be true. || means at least one side must be true. ! inverts a Boolean value. These operators are the foundation of readable decision-making code: instead of writing one large nested condition, you can name smaller facts and combine them.

At runtime, the CLR represents a bool as a small value type. You normally do not care about the exact storage size, because the runtime and compiler can choose efficient representation details, especially inside arrays, fields, and registers. What matters for C# code is the type rule: only a real Boolean expression can control a branch. The compiler enforces that rule before the program runs.

C# also has bool?, which means nullable Boolean. A nullable Boolean can hold true, false, or null. Use it only when unknown is a real third state, such as a survey answer that has not been submitted yet. For ordinary flags, prefer plain bool.

Syntax

int age = 20;
string name = "Maya";
bool isComplete = false;
bool hasAccess = true;
bool canContinue = hasAccess && !isComplete;
bool isAdult = age >= 18;
bool matches = name == "Maya";
bool? approved = null;
Part Meaning
bool Declares a variable that can contain only true or false.
true and false Boolean literals. They are lowercase keywords in C# source code.
&& Logical AND. The whole expression is true only when both operands are true.
|| Logical OR. The whole expression is true when at least one operand is true.
! Logical NOT. It flips true to false and false to true.
bool? A nullable Boolean that can also contain null.

Examples

Storing True-or-False Facts

using System;

class Program
{
    static void Main()
    {
        bool isOnline = true;
        bool hasPermission = false;
        bool canEdit = isOnline && hasPermission;

        Console.WriteLine($"Online: {isOnline}");
        Console.WriteLine($"Permission: {hasPermission}");
        Console.WriteLine($"Can edit: {canEdit}");
    }
}

Output:

Online: True
Permission: False
Can edit: False

This program stores two facts and combines them into a third fact. canEdit is false because && requires both operands to be true. Notice that Console.WriteLine prints Boolean values as True and False, even though C# source code uses lowercase true and false.

Building a Real Condition

using System;

class Program
{
    static void Main()
    {
        int age = 16;
        bool hasParentConsent = true;
        bool hasPaid = true;

        bool oldEnoughOrApproved = age >= 18 || hasParentConsent;
        bool canJoin = oldEnoughOrApproved && hasPaid;

        Console.WriteLine($"Approved by age or consent: {oldEnoughOrApproved}");
        Console.WriteLine($"Can join: {canJoin}");
    }
}

Output:

Approved by age or consent: True
Can join: True

The first expression is true because the person has parent consent, even though age >= 18 is false. The second expression is true because approval and payment are both true. Naming the intermediate value oldEnoughOrApproved makes the rule easier to read than one long expression.

Short-Circuit Evaluation

using System;

class Program
{
    static void Main()
    {
        int keyboardStock = 3;
        int mouseStock = 0;

        bool canShipKeyboard = keyboardStock > 0 && IsAvailable("keyboard");
        bool canShipMouse = mouseStock > 0 && IsAvailable("mouse");

        Console.WriteLine($"Keyboard: {canShipKeyboard}");
        Console.WriteLine($"Mouse: {canShipMouse}");
    }

    static bool IsAvailable(string item)
    {
        Console.WriteLine($"Checked {item}");
        return item == "keyboard";
    }
}

Output:

Checked keyboard
Keyboard: True
Mouse: False

The call for mouse never happens. With &&, C# stops as soon as the left side is false, because the whole expression cannot become true. This is called short-circuit evaluation, and it is useful for both performance and safety.

Nullable Boolean Values

using System;

class Program
{
    static void Main()
    {
        bool? emailConfirmed = null;
        Console.WriteLine($"Confirmed: {emailConfirmed == true}");

        emailConfirmed = false;
        Console.WriteLine($"Explicitly rejected: {emailConfirmed == false}");
    }
}

Output:

Confirmed: False
Explicitly rejected: True

A bool? can express unknown, but if still needs a plain bool. Comparing with == true or == false converts the nullable state into a definite answer.

How Booleans Work Step by Step

  1. The compiler assigns each Boolean literal, variable, and comparison expression the type bool.
  2. For comparisons such as age >= 18, it checks that the operator is valid for the operand types and emits code that produces a Boolean result.
  3. For && and ||, it emits branching logic so the right side may be skipped.
  4. For !, it emits logic that inverts the current Boolean value.
  5. When a Boolean controls an if or loop, the CLR follows one branch for true and another path for false.

The non-short-circuit operators & and | can also work with Boolean operands, but they always evaluate both sides. They are useful when you deliberately need both expressions to run, and they are also used as bitwise operators with integer values. In ordinary conditions, && and || are usually the clearer choice.

Common Mistakes

Using Numbers as Booleans

bool isReady = 1;

This does not compile. C# does not convert 1 to true. Use the Boolean literals directly, or compare a number to produce a Boolean.

bool isReady = true;
if (isReady)
{
    Console.WriteLine("Ready");
}

Output:

Ready

Accidentally Assigning Inside an if

bool isAdmin = false;
if (isAdmin = true)
{
    Console.WriteLine("Granted");
}
Console.WriteLine(isAdmin);

Output:

Granted
True

This compiles because assigning true to a bool produces a bool result. It is almost always a bug: the condition sets isAdmin to true instead of checking it. Use == if you need an explicit comparison, or simply write the Boolean variable by itself.

bool isAdmin = false;
if (isAdmin == true)
{
    Console.WriteLine("Granted");
}
else
{
    Console.WriteLine("Denied");
}

Output:

Denied

Forgetting That bool? Is Not bool

bool? acceptedTerms = null;
if (acceptedTerms)
{
    Console.WriteLine("Continue");
}

This does not compile because acceptedTerms might be null. Decide how unknown should behave, then convert it to a plain Boolean.

bool? acceptedTerms = null;
if (acceptedTerms == true)
{
    Console.WriteLine("Continue");
}
else
{
    Console.WriteLine("Stop");
}

Output:

Stop

Best Practices

  • Name Boolean variables as clear facts, such as isValid, hasAccess, canRetry, or shouldSave.
  • Prefer positive names when possible. isEnabled is usually easier to reason about than isNotDisabled.
  • Use && and || for normal conditions so short-circuiting protects later checks.
  • Break complex conditions into well-named intermediate Boolean variables.
  • Do not compare with == true for plain Booleans unless it improves clarity in a larger expression.
  • Use bool? only when unknown is meaningfully different from both yes and no.
  • Keep methods that return Booleans focused on one question, such as CanSubmitOrder or IsInRange.

Practice Exercises

  1. Create bool hasBadge and bool isEmployee. Print whether someone can enter when either value is true.
  2. Write a program with int temperature. Create a Boolean named isFreezing that is true when the temperature is less than or equal to zero.
  3. Create a nullable Boolean named surveyAnswered. Print Complete only when it is exactly true; otherwise print Incomplete.

Summary

  • bool is the C# type for true-or-false values.
  • Comparisons such as >, ==, and != produce Boolean results.
  • &&, ||, and ! combine or invert Boolean expressions.
  • C# does not treat numbers, strings, or objects as Booleans.
  • Short-circuiting means && and || may skip the right side.
  • bool? adds a third state, null, but branches still require a definite bool.
  • Readable Boolean names make conditions easier to understand and test.