TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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 as0x00).
Type Safety and Control Flow
C# enforces strict type safety regarding Boolean values. Unlike C or C++, there is no implicit or explicit conversion betweenbool 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.
Logical Operators
Thebool 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.
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.
Interop and Marshalling
When interacting with unmanaged code via Platform Invoke (P/Invoke) or COM, the memory representation ofbool 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.
Master C# with Deep Grasping Methodology!Learn More





