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 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.
Non-Short-Circuiting Logical OR (Boolean Types)
When applied tobool 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.
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.
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.
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.
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.
Master C# with Deep Grasping Methodology!Learn More





