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# is a binary operator that performs a bitwise logical AND operation on integral numeric types and enumeration types, and an unconditional logical AND operation on boolean types.
Bitwise AND (Integral Types)
When applied to integral numeric types (int, uint, long, ulong, nint, nuint), the & operator evaluates the binary representation of both operands and compares them bit by bit. The resulting bit is set to 1 if and only if the corresponding bits in both operands are 1. Otherwise, the resulting bit is set to 0.
Implicit Numeric Promotion
C# does not define built-in& operators that return smaller integral types (byte, sbyte, short, ushort, char). When the & operator is applied to these types, C# applies implicit numeric promotion. The operands are automatically promoted to int, and the result of the operation is an int.
If one operand is uint and the other is a signed integral type (sbyte, short, or int), both operands are promoted to long, and the result is a long.
Bitwise AND (Enumeration Types)
The& operator is fully supported for enum types. The operation is performed on the underlying integral type of the enumeration. This is mechanically essential for evaluating bitmask enumerations, typically decorated with the [Flags] attribute.
Unconditional Logical AND (Boolean Types)
When applied tobool operands, the & operator computes the logical AND. The result is true if both operands evaluate to true; otherwise, the result is false.
Unlike the conditional logical AND operator (&&), the & operator is non-short-circuiting. It strictly evaluates both the left-hand side (LHS) and right-hand side (RHS) expressions, regardless of the outcome of the LHS evaluation.
Nullable Boolean Logical AND (bool?)
When applied to nullable boolean (bool?) operands, the & operator implements three-valued logic. The evaluation rules are:
- Returns
trueif both operands aretrue. - Returns
falseif either operand isfalse, even if the other operand isnull. - Returns
nullotherwise (i.e., if one operand istrueand the other isnull, or if both arenull).
null literals in these expressions to resolve the operator overload.
Compound Assignment
The& operator supports compound assignment via the &= operator. An expression using &= evaluates the bitwise or logical AND of the operands and assigns the result to the left operand.
Operator Overloading
User-defined types (classes and structs) can overload the& operator to define custom evaluation logic. When a user-defined type overloads the & operator, the &= operator is implicitly overloaded as well.
Master C# with Deep Grasping Methodology!Learn More





