Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The bool keyword in C# is a value type that represents a Boolean logical quantity. It is an alias for the .NET System.Boolean structure and can hold exactly one of two mutually exclusive literal values: true or false.

Memory and Initialization

As a value type, bool is allocated on the stack (when used as a local variable) or inline within its containing type.
  • Size: 1 byte (8 bits). Although a boolean state theoretically requires only a single bit, the CLR allocates a full byte because it is the smallest addressable unit of memory.
  • Default Value: false (represented in memory as 0x00).
class StateManager
{
    private bool defaultState; // Implicitly initialized to false as a class field

    public void EvaluateState()
    {
        bool isActive = true;
        bool isComplete = false;
        // Local variables must be explicitly initialized before use
    }
}

Type Safety and Control Flow

C# enforces strict type safety regarding Boolean values. Unlike C or C++, there is no implicit or explicit conversion between bool and integer types. An integer value of 1 does not equate to true, and 0 does not equate to false. Because of this strict typing, bool is the required type for conditions in C# control flow statements (such as if, while, for, and do). You cannot evaluate an integer directly as a condition; it must be explicitly evaluated into a boolean expression.
using System;

class TypeSafetyExample
{
    public void Evaluate()
    {
        // INVALID: Cannot implicitly convert type 'int' to 'bool'
        // bool invalidImplicitFlag = 1; 

        // INVALID: Cannot explicitly cast 'int' to 'bool'
        // bool invalidExplicitFlag = (bool)1; 

        // VALID: Requires explicit evaluation or the Convert class
        bool validEvaluatedFlag = (1 == 1); 
        bool flagFromInt = Convert.ToBoolean(1); // Returns true

        // VALID: Control flow requires a boolean expression
        if (validEvaluatedFlag)
        {
            // Execution block
        }
    }
}

Logical Operators

The bool type supports several logical operators for evaluating expressions:
  • Unary: ! (logical negation).
  • Binary (Short-circuiting): && (conditional logical AND), || (conditional logical OR). These operators bypass the evaluation of the right-hand operand if the overall result can be determined entirely by the left-hand operand.
  • Binary (Non-short-circuiting): & (logical AND), | (logical OR), ^ (logical exclusive OR). These operators always evaluate both operands.
class LogicalOperatorsExample
{
    public void EvaluateLogic()
    {
        bool a = true;
        bool b = false;

        bool notA = !a;          // false
        bool andResult = a && b; // false (short-circuits if 'a' is false)
        bool orResult = a || b;  // true (short-circuits if 'a' is true)
        bool xorResult = a ^ b;  // true (evaluates both operands)
    }
}

Nullable Boolean

By default, bool cannot represent an undefined state. To accommodate a third state, C# provides the nullable boolean type, denoted by bool?. This is syntactic sugar for System.Nullable<System.Boolean>. A bool? can hold three distinct values: true, false, or null.
class NullableExample
{
    public void CheckState()
    {
        bool? isConfigured = null;

        if (isConfigured.HasValue)
        {
            bool underlyingValue = isConfigured.Value;
        }
    }
}

Interop and Marshalling

When interacting with unmanaged code via Platform Invoke (P/Invoke) or COM, the memory representation of bool can change. By default, the CLR marshals a managed bool as a 4-byte Win32 BOOL type. If the unmanaged API expects a 1-byte C++ bool, you must explicitly define the marshalling behavior using the [MarshalAs] attribute.
using System.Runtime.InteropServices;

class InteropExample
{
    // Marshals as a 1-byte unmanaged type
    [DllImport("NativeLib.dll")]
    public static extern void ProcessData([MarshalAs(UnmanagedType.I1)] bool flag);
}
Master C# with Deep Grasping Methodology!Learn More