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.
& operator in C# functions as either a logical AND operator for boolean expressions or a bitwise AND operator for integral numeric types. Unlike the conditional logical AND operator (&&), the & operator enforces strict evaluation, meaning it always evaluates both operands regardless of the first operand’s resulting value.
Logical AND (Boolean Operands)
When applied tobool operands, the & operator computes the logical AND of its operands. The result is true if and only if both operands evaluate to true. Because it lacks short-circuiting behavior, any side effects present in the right-hand operand will always execute.
Nullable Boolean Logical AND
When applied to nullable boolean (bool?) operands, the & operator implements three-valued logic. The operation evaluates to false if either operand is false, even if the other operand is null. If both operands are true, the result is true. In all other combinations involving null, the result is null. When applying the operator to two null literals, at least one operand must be explicitly cast to bool? to resolve compiler ambiguity (CS0034).
Bitwise AND (Integral Operands)
When applied to integral numeric types (sbyte, byte, short, ushort, int, uint, long, ulong, nint, nuint) or enumeration types (enum), the & operator performs a bit-by-bit logical AND operation. It compares the binary representation of both operands. A bit in the result is set to 1 if and only if the corresponding bits in both operands are 1; otherwise, the resulting bit is 0.
Type Promotion and Evaluation
For integral types smaller thanint (such as byte or short), C# applies implicit numeric promotion before performing the bitwise operation. The operands are converted to int (or uint, depending on the types), the bitwise AND is performed at the 32-bit level, and the return type of the operation is int. Explicit casting is required to assign the result back to the smaller integral type.
Compound Assignment
The& operator can be combined with the assignment operator (=) to form the compound assignment operator &=. This evaluates the & operation and assigns the result back to the left-hand variable. For integral types smaller than int, the compound assignment operator automatically applies an explicit cast back to the target type, preventing the CS0266 compiler error that would occur with standard assignment due to implicit numeric promotion.
Operator Overloading
User-defined types (classes and structs) can overload the& operator to define custom behavior for instances of that type. When a binary operator is overloaded, the corresponding compound assignment operator (if any) is implicitly overloaded.
Master C# with Deep Grasping Methodology!Learn More





