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 is a compound assignment operator that performs a bitwise AND or logical AND operation between its left-hand and right-hand operands, subsequently assigning the resulting value to the left-hand operand.
The expression x &= y is generally evaluated as x = x & y, subject to two strict technical distinctions:
- The left-hand operand
xis evaluated only once. - For integral types smaller than
int(such asbyte,sbyte,short,ushort, andchar), the binary&operator applies numeric promotion, converting the operands toint. The&=operator automatically performs a hidden explicit cast back to the original typeT(evaluating asx = (T)(x & y)) only ifyis implicitly convertible toT. Ifyis not implicitly convertible toT(for example, ifyis anintvariable), the compiler emits error CS0266 rather than automatically casting.
Type-Specific Behavior
The underlying operation of the&= operator depends strictly on the data types of its operands:
1. Integral Types
When applied to integral numeric types (int, uint, long, ulong, short, ushort, byte, sbyte, char, nint, nuint), the operator performs a bitwise AND. It compares the binary representations of both operands bit by bit. A bit in the result is set to 1 if and only if the corresponding bits in both operands are 1; otherwise, it is set to 0.
2. Boolean Types
When applied tobool operands, the operator performs a logical AND. It evaluates to true if and only if both operands are true. Unlike the && operator, the & operation within &= is non-short-circuiting; the right-hand operand is always evaluated regardless of the left-hand operand’s state.
3. Nullable Value Types
The&= operator supports lifted operators for nullable value types (T?). For nullable integral types, the operation evaluates to null if either operand is null.
For the nullable boolean type (bool?), the &= operator applies three-valued logic, adhering to SQL-like boolean semantics:
false &= nullevaluates tofalse(becausefalse & <any>is alwaysfalse).true &= nullevaluates tonull.null &= nullevaluates tonull.
4. Enumerations
When applied toenum types, the operator performs a bitwise AND operation on the underlying integral values of the enumeration members.
Operator Overloading
The&= operator cannot be explicitly overloaded. However, if a user-defined type overloads the binary & operator, the compound assignment operator &= is implicitly evaluated using that overload.
Master C# with Deep Grasping Methodology!Learn More





