Site Search
Homepage of Otaku No Zoku
Complete Archives of Otaku No Zoku
About Otaku No Zoku
Subscribe to Otaku No Zoku
Bookmark Otaku No Zoku

C# What Is The Bool Data Type :

.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.

C# bool Type Definition

The 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.

Handling The bool Data Type

This program demonstrates how to assign a value to a Boolean variable by performing a test with the ternary operator.

The Steps

  1. Declare an integer variable that represents the player’s hit points.
  2. Determine if the player is dead and assign the result of true or false to a bool value.
  3. Print out a simple message dependent on whether the player is dead or alive.

The Source Code

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.");
    }

  }

}

Program Output

Yep, player is dead, time to begin that corpse run.

Results

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.

Summary

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.

Liked This Post?

Subscribe to the RSS feed or follow me on Twitter to stay up to date!