Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The & 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:
  1. 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 string or a class reference) results in compiler error CS0208.
  2. 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 a ref-returning property.
  3. 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 fixed statement. This prevents the garbage collector from relocating the variable. Attempting to use & directly on an unfixed movable variable results in compiler error CS0212.
using System;

// Requires compiling with the /unsafe flag
unsafe
{
    // Local variable (fixed variable, resides on the stack)
    int localNumber = 42;
    int* pointerToLocal = &localNumber; 
    
    // Movable variable (resides on the managed heap)
    int[] array = { 10, 20, 30 };
    
    // Must be pinned using 'fixed' to take the address
    fixed (int* pointerToArrayElement = &array[0])
    {
        Console.WriteLine(*pointerToArrayElement); // Outputs: 10
    }
}

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.
using System;

bool a = false;
bool b = false;

// The right-hand side (b = true) is evaluated even though 'a' is already false.
bool result = a & (b = true); 

Console.WriteLine(result); // Outputs: False
Console.WriteLine(b);      // Outputs: True

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.
using System;

bool? nullBool = null;

bool? a = true & nullBool;     // Evaluates to null
bool? b = false & nullBool;    // Evaluates to false
bool? c = nullBool & nullBool; // Evaluates to null
bool? d = true & (bool?)null;  // Evaluates to null

Console.WriteLine(a.HasValue); // Outputs: False
Console.WriteLine(b);          // Outputs: False
Console.WriteLine(c.HasValue); // Outputs: False
Console.WriteLine(d.HasValue); // Outputs: False

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.
using System;

uint a = 0b_1100_1010; // Decimal: 202
uint b = 0b_1010_1100; // Decimal: 172

uint result = a & b;   // 0b_1000_1000 (Decimal: 136)

Console.WriteLine(result); // Outputs: 136

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 as byte, sbyte, short, ushort, and char) are always implicitly promoted to int, even if both operands are of the exact same type. For example, in the expression byte & byte, both operands are promoted to int, and the resulting value is an int, not a byte. If the operands are of different types, they are promoted to the smallest common type that can contain both (e.g., an int and a long are both promoted to long).
  • 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 (class or struct) 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 type T overloads the binary & operator such that it takes exactly two parameters of type T and returns type T (i.e., public static T operator &(T x, T y)), and the type also overloads the true and false operators, it implicitly enables support for the short-circuiting conditional logical AND operator (&&) for that specific type.
Master C# with Deep Grasping Methodology!Learn More