TExcelTAD
Home › C# › C# Booleans

C# Booleans

Programs constantly make yes/no decisions: Is the user logged in? Is the cart empty? Has the timer expired? C# represents such answers with the bool type, which has exactly two values: true and false.

Boolean variables

csharp · try it yourself
using System;

bool isRaining = false;
bool hasUmbrella = true;

Console.WriteLine(isRaining);
Console.WriteLine(hasUmbrella);
Console.WriteLine(isRaining.GetType().Name);
False
True
Boolean

bool is an alias for System.Boolean. When printed, the values appear as True and False, but in code the keywords are always lowercase.

Note: Unlike C or JavaScript, C# does not treat numbers or strings as booleans. if (1) or if ("yes") will not compile — the condition must be a real bool.

Boolean expressions

Most booleans are not written as literals; they are the result of a comparison.

csharp · try it yourself
using System;

int speed = 72;
int limit = 50;

bool tooFast = speed > limit;
Console.WriteLine(tooFast);
Console.WriteLine(speed == 72);
Console.WriteLine(speed != limit);
Console.WriteLine(10 <= 9);
Console.WriteLine("abc" == "abc");
True
True
True
False
True

Combining conditions

Use logical operators to build more complex conditions. A small "truth table" shows every combination:

a b a && b a || b !a a ^ b
true true true true false false
true false false true false true
false true false true true true
false false false false true false
csharp · try it yourself
using System;

int age = 16;
bool withParent = true;
bool isMember = false;

bool canWatch = age >= 18 || withParent;
bool getsDiscount = isMember && age < 18;
bool needsId = !withParent;

Console.WriteLine(
quot;Can watch: {canWatch}"); Console.WriteLine(
quot;Gets discount: {getsDiscount}"); Console.WriteLine(
quot;Needs ID: {needsId}");
Can watch:      True
Gets discount:  False
Needs ID:       False

Real-world example: password rules

Giving each condition a well-named variable makes complex rules easy to read and to test:

csharp · try it yourself
using System;

string password = "Secret42";

bool longEnough = password.Length >= 8;
bool hasDigit = false;
bool hasUpper = false;

foreach (char c in password)
{
    if (char.IsDigit(c)) hasDigit = true;
    if (char.IsUpper(c)) hasUpper = true;
}

bool isStrong = longEnough && hasDigit && hasUpper;
Console.WriteLine(
quot;Length ok: {longEnough}, digit: {hasDigit}, upper: {hasUpper}"); Console.WriteLine(isStrong ? "Strong password" : "Weak password");
Length ok: True, digit: True, upper: True
Strong password

Converting to and from bool

Expression Result
bool.Parse("True") true (case-insensitive)
bool.TryParse("yes", out var b) false — only "true"/"false" are accepted
true.ToString() "True"
Convert.ToBoolean(0) false (any non-zero number is true)

Tip: Name boolean variables as questions or statements: isVisible, hasErrors, canEdit. Code like if (hasErrors) then reads like English.

Test yourself

What does Console.WriteLine(5 > 3 && 2 > 4); print?

  1. True
  2. False
  3. 1
  4. 0

Which statement compiles in C#?

  1. if (1) { }
  2. if ("true") { }
  3. if (count > 0) { }
  4. if (count) { } // count is int