C# bool (Boolean)
The bool data type is used to store two possible values: true or false. It is often used for conditional logic, such as determining whether a condition is met.
Key Concepts
- The
booltype stores only two values:trueorfalse. - Booleans are frequently used in
ifstatements, loops, and other control structures. - The default value for a
boolisfalse.
Example of Using a bool
Code Example
// Declare a boolean variable
bool isTrue = true;
// Output the boolean to the console
Console.WriteLine(isTrue);
Output:
True
Code Explanation: The bool variable isTrue is initialized with the value true. It is printed using the Console.WriteLine() method.
Output Explanation: The boolean value true is displayed on the console, which is the value assigned to isTrue.