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 inclusive OR for integral numeric types, a non-short-circuiting logical OR for boolean types, and supports three-valued logic for nullable booleans. It strictly evaluates both operands, returning a result where a bit or boolean state is 1 or true if at least one of the corresponding operands evaluates to 1 or true.

Bitwise Logical OR (Integral Types)

For integral numeric types, the | operator computes the bitwise inclusive OR by comparing each bit of the first operand to the corresponding bit of the second operand. The operator is predefined for int, uint, long, ulong, nint, and nuint. Numeric Promotion When applied to integral types smaller than int (byte, sbyte, short, ushort), C# applies implicit numeric promotion. Both operands are implicitly converted to int before the operation, and the resulting value is of type int. Assigning the result back to the smaller type requires an explicit cast.
uint a = 0b_1010_0000;
uint b = 0b_1001_0001;
uint c = a | b; // 0b_1011_0001

byte x = 1;
byte y = 2;
// byte z = x | y;         // Compilation error: Cannot implicitly convert type 'int' to 'byte'
byte z = (byte)(x | y);    // Requires explicit cast

Non-Short-Circuiting Logical OR (Boolean Types)

When applied to bool operands, the | operator computes the logical inclusive OR. Unlike the conditional logical OR operator (||), the | operator strictly evaluates both the left-hand and right-hand operands, regardless of the left-hand operand’s value.
bool ReturnTrue() 
{
    Console.WriteLine("Evaluated");
    return true;
}

bool a = true;
bool b = a | ReturnTrue(); 

// Result: b is true.
// "Evaluated" is printed to the console because ReturnTrue() executes 
// even though 'a' is already true.

Nullable Boolean Logical OR

When applied to nullable boolean types (bool?), the | operator implements three-valued logic. The result evaluates to true if either operand is true. If neither operand is true and at least one operand is null, the result evaluates to null. The null literal cannot be used directly with a boolean literal without an explicit cast to bool? or assignment to a nullable variable.
bool? trueVal = true;
bool? falseVal = false;
bool? nullVal = null;

bool? a = trueVal | nullVal;   // Evaluates to true
bool? b = falseVal | nullVal;  // Evaluates to null
bool? c = nullVal | nullVal;   // Evaluates to null

// Alternatively, using explicit casts with literals:
bool? d = false | (bool?)null; // Evaluates to null

Enumeration Logical OR

The | operator is natively supported by enumeration types. It performs the bitwise OR operation on the underlying integral values of the enumeration members, returning a strongly typed enum result.
enum State : byte 
{ 
    None = 0, 
    Active = 1, 
    Visible = 2 
}

State s = State.Active | State.Visible;
// Underlying integer value is 3

Operator Overloading

User-defined types (class, struct, or record) can overload the | operator to define custom behavior. When a binary operator is overloaded, the corresponding compound assignment operator (|=) is implicitly overloaded.
public struct BitMask
{
    public int Value { get; }
    public BitMask(int value) => Value = value;

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

BitMask mask1 = new BitMask(1);
BitMask mask2 = new BitMask(2);
BitMask result = mask1 | mask2; // result.Value is 3

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 evaluated only once.
int value = 4;      // 0b_0100
value |= 2;         // 0b_0010
// value is now 6   // 0b_0110

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