Skip to main content

Booleans

C# has several numeric types (int, double) but only one logical type: bool. A bool can only hold one of two values: true or false.

This makes it perf for "yes/no" decisions in a program. For example:

  • If age is greater than or equal to 18, print "You can vote."
  • If age is less than 18, print "Sorry, you can't vote yet."

Naming Conventions​

Boolean variables are typically prefixed with is, has, or can to signal that they represent a true/false state:

bool isRaining = true;
bool hasLicense = false;
bool canEdit = false;

By convention, AVOID naming variables with built-in negation (e.g., isNotRaining, hasNoLicense). Instead, use the ! operator to negate a positively-named variable when needed:

Console.WriteLine(!isRaining); // not raining
Console.WriteLine(!hasLicense); // does not have license
Console.WriteLine(!canEdit); // can not edit

Negatively-named variables become confusing when negated themselves, for example !isNotRaining ("not not raining") is harder to read than isRaining.