.NET and C# provides the programmer with the bool
(Boolean) data type to represent values that are true
or false
. Any need to represent a two-state mechanism, such as a light switch, can be represented by a bool
, e.g. on or off, yes or no, and so on.
bool
Type DefinitionThe intrinsic data type bool
permits you to store Boolean values of true
or false
. bool
is an alias of the System.Boolean
data structure which offers functionality for performing comparisons and parsing from strings, amongst other things.
To declare a simple Boolean variable, use:
bool playerInCombatLockdown;
Which because of the default value of bool
being false
(more on that in a moment), the value of playerInCombatLockdown
would also be false
.
Alternatively, to declare and assign a value to the variable, use:
bool playerInCombatLockdown = true;
Which assigns the value true
to the bool
variable when it is declared.
If you need to store a Boolean variable that can also handle a null
value in addition to true
and false
, such as you would if reading from a database table that may contain undefined values, you can make use of the nullable type
, bool?
.
Just as you would declare a regular bool
, the nullable bool?
can be declared so:
bool? playerIsAlive;
And of course, assigning a value to the nullable bool?
:
bool? playerIsAlive = false;
The default value of a bool
variable is set to false
and the default value of a nullable bool?
is null
. So note that in the examples given above, the first declaration of a regular bool
without assignment would have a value of false
, but the declaration of the nullable bool?
type would have a value of null
and they would not be equivalent if tested. To make them equivalent, you would have to use:
bool playerIsAlive;
bool? playerInCombatLockdown = false;
bool equal = (playerIsAlive == playerInCombatLockdown);
And whilst the code itself would compile and work, and the equal
variable would evaluate to true
because both variables tested have the same value of false
, the actual comparison itself makes no sense as why you would want to compare if the player is alive with whether they are in combat lockdown is puzzling to say the least.
This program demonstrates how to assign a value to a Boolean variable by performing a test with the ternary operator.
integer
variable that represents the player’s hit points. true
or false
to a bool
value. using System;
class BooleanTest
{
static void Main()
{
// declare an integer variable that represents the player's hit points
int playerHitsPoints = -1;
// determine if the player is dead and assign the result to a bool value
bool isDead = playerHitsPoints <= 0 ? true : false;
// print out a simple message dependent on whether the player is dead or alive.
if (isDead == true)
{
Console.WriteLine("Yep, player is dead, time to begin that corpse run.");
}
else
{
Console.WriteLine("Player is still alive. We fight on.");
}
}
}
Yep, player is dead, time to begin that corpse run.
The player’s hit points in the example program begin at negative one (must have rolled a priest), and the ternary operator tests the hit points to see if they are at or below zero. Because in this case the hit points are below zero, the player is flagged as being dead and the appropriate message indicating a dead player is printed to the console.
The test for whether the player is dead, using the ternary operator, could of course be replaced with:
bool isDead = (playerHitPoints <= 0);
Which would have returned the exact same result. The given source code is a trivial example to explicitly demonstrate the use of bool
so obviously it is not the most efficient.
The bool
data type and its close acquiantance the nullable bool?
data type are intrinsically simplistic variable types but like all instrinsic data types they are one of the work horses of the C# language and .NET framework that you will make use of often.