The bool Type
A bool is a light switch that can only be on or off, true or false. Programs use it to answer yes-or-no questions such as 'is the door open?'.
In this page:
Syntax
bool variableName = true;
bool result = value1 >= value2;
The bool Type
A bool holds either true or false and is the result of every comparison and logical expression. Booleans drive if statements, loops, and conditional logic throughout C#. Unlike some languages, C# never implicitly converts numbers to booleans.
Note:
Comparison operators like
==, >, and < always evaluate to a bool.Example: The bool Type
using System;
class Program
{
static void Main(string[] args)
{
bool isActive = true;
bool hasPermission = false;
bool isAdult = 20 >= 18;
Console.WriteLine(isActive);
Console.WriteLine(hasPermission);
Console.WriteLine(isAdult);
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Writing
if (x = true)instead ofx == trueor justif (x). - Expecting numbers to work as booleans, when C# does not treat 0 or 1 as
falseortrue. - Writing
TrueorTRUEinstead oftrue, since C# keywords are lowercase.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: