HomeSearch

C# bool Type: If True, False

Test the bool type, which holds true or false. A bool occupies 1 byte of memory.
Bool. Day and night are parts of a boolean condition. Day is true (it has light) and night is false. This can be stored in a bool variable.
Usage notes. Bool is often used in expressions. Many expressions evaluate to a boolean value. Represented in one byte, the bool type represents truth.Byte
First program. A bool can be assigned to the true and false literals. We set the bool variable to true. And then we invert the value of the bool using the exclamation operator.True, False

Info: The word bool comes from George Boole, a mathematician who pioneered Boolean logic in the 1800s in England.

C# program that uses bool 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
Bool variable. This program declares a bool variable. It manipulates its values through expressions. The size of the bool type is one byte. It is aliased to the System.Boolean type.

Literals: The true and false literals are hard-coded values. True and false also have the TrueString and FalseString representations.

TrueString, FalseString

Invert: The program's second assignment sets the variable value equal to its opposite value.

Tip: This means that a true value will become a false value. This technique is useful for setting bool preferences.

Also: It is possible, but often not necessary, to define a custom Flip() method to do this.

C# program that uses bool in many contexts 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
Notes, continued. Often, if-statements test an expression directly. But they can also test a bool. In an if-statement, the expression is evaluated in a Boolean context.

And: We can store the result of an expression evaluation as a local variable. This technique can simplify complex logic tests.

Ints, bools. How can you convert an int in your C# program to a bool value? This can be done with the Convert.ToBoolean method in the System namespace. A custom method could also be used.Int, uintBool to Int

Here: This program uses the Convert type to convert ints into bools. The Convert.ToBoolean method is perfect for this purpose.

Info: This method considers 0 to be false. All other values, positive and negative, are true.

C# program that converts int to bool using System; class Program { static void Main() { Console.WriteLine(Convert.ToBoolean(5)); Console.WriteLine(Convert.ToBoolean(0)); Console.WriteLine(Convert.ToBoolean(-1)); } } Output True False True
Null bool. A bool cannot be null—its default value is false. But we can use a nullable bool, which is a struct that wraps a bool value, and set it to null.

Initialized: The null value of a nullable bool can be used to mean uninitialized. So we can have a 3-state bool here.

Nullable
C# program that uses nullable bool using System; class Program { static void Main() { // A null bool can mean an uninitialized boolean value. bool? valid = null; Console.WriteLine("Initialized: {0}", valid.HasValue); // We can then set the nullable bool to true or false. valid = true; Console.WriteLine("Valid: {0}", valid.Value); } } Output Initialized: False Valid: True
Usage. Bools can be used in a variety of program contexts to represent truth values. They are used throughout programs—even if you do not notice them.bool: Arraybool: Array Sortbool: Parse

Methods: In C# programs, methods often return bool values. These methods sometimes have the prefix "Is" on their names.

bool: Return

Also: Properties can return bool values. The book Code Complete shows how boolean expressions are simplified.

Research. I learned some technical specifics of bool in the CLI specification. If the byte has all zeros, it means false. And if any of the four bits in that byte are set to 1, it is true.

Quote: 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 (The CLI Annotated Standard).

Nearly every C# program uses bools. All if-statements requires a boolean expression. We explored bools, established their representation size in bytes, and tested conversions.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls