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 both a unary operator that returns the memory address of its operand, and a binary operator that performs either a logical AND operation on boolean types or a bitwise AND operation on integral numeric types. Unlike the conditional logical AND operator (&&), the binary & operator is non-short-circuiting. It evaluates operands strictly from left to right and will evaluate the right-hand operand regardless of the left-hand operand’s resulting boolean value, provided the left-hand evaluation does not halt execution by throwing an exception.
Unary Address-Of Operator (unsafe)
When used as a unary operator (&operand), it acts as the address-of operator. It returns a pointer to the memory address of the variable it is applied to. This operation requires an unsafe context and the project must be compiled with the /unsafe compiler flag.
The operand must satisfy strict type and memory constraints:
- Unmanaged Type Constraint: The operand must be of an unmanaged type (e.g., primitive numerics, enums, pointers, or structs containing only unmanaged fields). Attempting to take the address of a managed type (such as a
stringor aclassreference) results in compiler errorCS0208. - Memory Location Constraint: The operand must be a variable, an array element, or a field. It cannot be a constant, a value, or a standard property access (which are implemented as methods and lack a direct memory address, yielding
CS0211). C# 7.0 and later does permit taking the address of aref-returning property. - Pinning Movable Variables: If the operand is a “movable” variable residing on the managed heap (such as an array element or a field of a reference type), taking its address requires pinning it in memory using the
fixedstatement. This prevents the garbage collector from relocating the variable. Attempting to use&directly on an unfixed movable variable results in compiler errorCS0212.
Binary Logical AND Evaluation (bool)
When both operands are of type bool, the binary & operator computes the logical AND. The result is true if and only if both operands evaluate to true; otherwise, the result is false. Because it lacks short-circuiting behavior, any assignments or side effects present in the right-hand operand are evaluated even if the left-hand operand evaluates to false.
Nullable Boolean Logical AND (bool?)
When applied to nullable booleans (bool?), the & operator implements three-valued logic. The operator evaluates to false if either operand is false, regardless of whether the other operand is null. If neither operand is false and at least one operand is null, the result is null. It evaluates to true only if both operands are true.
Because the null literal does not have an inherent type in C#, it must be explicitly cast or assigned to a bool? variable before applying the operator to avoid compiler error CS0019.
Binary Bitwise AND Evaluation (Integral Types)
When applied to integral numeric types (sbyte, byte, short, ushort, int, uint, long, ulong, nint, nuint) or char, the & operator computes the bitwise AND of its operands. It evaluates the binary representation of the operands, comparing each bit at the corresponding position. The resulting bit is set to 1 if both corresponding bits in the operands are 1; otherwise, the resulting bit is set to 0.
Type Promotion and Evaluation Rules
- Numeric Promotion: C# applies binary numeric promotion to integral operands before performing the bitwise operation. Integral types smaller than
int(such asbyte,sbyte,short,ushort, andchar) are always implicitly promoted toint, even if both operands are of the exact same type. For example, in the expressionbyte & byte, both operands are promoted toint, and the resulting value is anint, not abyte. If the operands are of different types, they are promoted to the smallest common type that can contain both (e.g., anintand alongare both promoted tolong). - Enumerations: The
&operator is supported for enumeration (enum) types. The operation is performed at the bit level on the underlying integral values of the enumeration members. The return type is the enumeration type itself. - Operator Overloading: User-defined types (
classorstruct) can overload the&operator to define custom behavior. When a type overloads the binary&operator, the compound assignment operator (&=) is implicitly overloaded as well. Critically, if a user-defined typeToverloads the binary&operator such that it takes exactly two parameters of typeTand returns typeT(i.e.,public static T operator &(T x, T y)), and the type also overloads thetrueandfalseoperators, it implicitly enables support for the short-circuiting conditional logical AND operator (&&) for that specific type.
Master C# with Deep Grasping Methodology!Learn More





