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 a bitwise OR operator for integral and enumeration types, and as a non-short-circuiting logical OR operator for boolean types. Its behavior is determined by the type of its operands, and it can be customized for user-defined types via operator overloading.

Bitwise OR (Integral Types)

When applied to integral numeric types (int, uint, long, ulong, short, ushort, byte, sbyte, nint, nuint), the | operator performs a bit-by-bit logical OR operation. It compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the resulting bit is set to 1; if both are 0, the resulting bit is 0. Numeric Promotion: When applying the | operator to smaller integral types (byte, sbyte, short, ushort), C# applies implicit numeric promotion. The operands are converted to int, and the operation evaluates to an int. Assigning the result back to the smaller type requires an explicit cast.
int a = 5;          // Binary: 0000 0101
int b = 3;          // Binary: 0000 0011
int result = a | b; // Binary: 0000 0111 (Decimal: 7)

byte c = 1;
byte d = 2;
// Numeric promotion requires an explicit cast to assign back to byte
byte byteResult = (byte)(c | d); 

Logical OR (Boolean Types)

When applied to bool operands, the | operator computes the logical OR of the expressions. It returns true if at least one operand evaluates to true; otherwise, it returns false. Unlike the conditional logical OR operator (||), the | operator guarantees strict evaluation. It evaluates both the left-hand side and right-hand side operands regardless of the left-hand side’s result, meaning it does not short-circuit. Nullable Boolean Types: For bool? operands, the | operator implements three-valued logic. If either operand is true, the result is true. If one operand is false and the other is null, the result is null.
bool lhs = true;
bool rhs = false;
bool strictResult = lhs | rhs; // Evaluates to true, both sides are evaluated

// Three-valued logic with bool?
bool? nullBool = null;
bool? trueResult = true | nullBool;   // Evaluates to true
bool? nullResult = false | nullBool;  // Evaluates to null

Enumeration Types

When applied to operands of the same enum type, the | operator performs a bitwise OR operation on the underlying integral values of the enumeration members. This is mechanically identical to the integral bitwise OR but maintains the strong typing of the enumeration.
[Flags]
public enum FileAccess
{
    None = 0,      // 0000
    Read = 1,      // 0001
    Write = 2,     // 0010
    Execute = 4    // 0100
}

FileAccess access = FileAccess.Read | FileAccess.Write; 
// Underlying math: 1 | 2 = 3 (Binary 0011)

Operator Overloading

The | operator can be overloaded to provide custom behavior for user-defined types (classes and structs). When a user-defined type overloads the | operator, it defines the specific return type and evaluation logic for instances of that type.
public struct CustomBitset
{
    public int Value { get; }
    public CustomBitset(int value) => Value = value;

    public static CustomBitset operator |(CustomBitset left, CustomBitset right)
    {
        return new CustomBitset(left.Value | right.Value);
    }
}

CustomBitset set1 = new CustomBitset(4);
CustomBitset set2 = new CustomBitset(8);
CustomBitset combined = set1 | set2;

Compound Assignment

The | operator can be combined with the assignment operator to form the |= compound assignment operator. The expression x |= y is evaluated as x = x | y, except that x is only evaluated once.
int mask = 8;       // Binary: 1000
mask |= 4;          // Binary: 1100 (Decimal: 12)

bool flag = false;
flag |= true;       // Evaluates to true
Master C# with Deep Grasping Methodology!Learn More