C# Bool Type

Value Bool

Bool keyword

The bool variable type stores true and false. Bool variables can be assigned values based on expressions. Many expressions evaluate to a boolean value. Represented in one byte, the bool type in the C# language conveniently represents truth.

Keywords

A CLI Boolean type occupies 1 byte in memory. A bit pattern of all zeros denotes a value of false. A bit pattern with any bit set (analogous to a non-zero integer) denotes a value of true. Miller & Ragsdale, p. 479

Introduction

First, the bool type can be assigned the true and false literals. In this introductory program, we assign the true literal and then invert the value of the bool using the ! operator.

Program that uses bool [C#]

using System;

class Program
{
    static void Main()
    {
	bool val = true;
	if (val)
	{
	    Console.WriteLine(val == true);
	}
	val = !val;
	if (!val)
	{
	    Console.WriteLine(val == false);
	}
    }
}

Output

True
True

Example 2

Next, this program declares a bool variable and assigns it to the true literal, and then manipulates its values through expressions and also evaluates its value. Also, the program shows the size of the bool type is one byte and it aliased to the System.Boolean type.

Program that uses bool in many contexts [C#]

using System;

class Program
{
    static void Main()
    {
	// Use a local variable of type bool.
	// ... Write its value.
	// ... Then invert its value with unary not.
	// ... Then set to false.
	bool value = true;
	Console.WriteLine(value);

	value = !value;
	Console.WriteLine(value);

	value = false;
	Console.WriteLine(value);

	// You can test the boolean value in an if-statement.
	// ... Only the second if statement body is reached.
	if (value)
	{
	    Console.WriteLine("Not reached");
	}
	if (!value)
	{
	    Console.WriteLine("Reached");
	}

	// You can use a bool local variable to store an expression result.
	// ... This simplifies some code.
	bool test = !value &&
	    1 == int.Parse("1");
	Console.WriteLine(test);

	// Print the bool size in bytes and its type.
	Console.WriteLine(sizeof(bool));
	Console.WriteLine(true.GetType());
    }
}

Output

True
False
False
Reached
True
1
System.Boolean
True keyword

Description. The program demonstrates the assignment of the true and false literals to the bool variable. The true and false literals are simply the hard-coded values "true" and "false"; such hard-coded values are considered literals. True and false also have the TrueString and FalseString representations.

True Value False Value bool.TrueString and FalseString

Invert boolean. The program's second assignment sets the variable value equal to its opposite value. This means that a true value will become a false value. This technique is useful for setting preferences that are represented as bool fields. It is also possible to define a Flip() method to do this.

If keyword

If-statement. Most often, if-statements test an expression directly, but they can also test a bool variable storage location. In an if-statement, the expression is always evaluated in a Boolean context; the bool variable is already stored in a Boolean context. Sometimes, this reduces the work required.

If Statement

Assign bool to expression result. You can store the result of an expression evaluation as a local variable or field directly. This technique can simplify complex logic tests in your code. Also, it can reduce the number of local variables, making the program's model simpler.

Tips

Note (please read)

Bools can be used in a variety of program contexts to represent truth values. Because they are so common, it is good to see some examples of how they can be used.

bool.Parse Examples Bool Array Memory Convert Bool to Int Bool Sort Example

Bool methods. Methods in C# programs often return bool values. These methods sometimes have the prefix Is on their name; such as IsStartElement. Properties can also return bool values. The book Code Complete by Steve McConnell provides examples on how boolean expressions can and should be simplified; please see this book for more advanced tutorials on bool methods.

Bool Methods, Return True and False Code Complete: Book Review

George Boole

The word bool is named after George Boole, who pioneered this sort of logic in the middle part of the 1800's in England. For this reason, some texts prefer to capitalize the word Boolean, but many do not.

Summary

The C# programming language

We explored the bool type in the C# language, demonstrating its basic usage through an example program and establishing its representation size in bytes. Bool variables can be used as intermediate storage locations for expression evaluations, and in this way can reduce complexity in complex conditional logic.

Dot Net Perls