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 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.
Logical OR (Boolean Types)
When applied tobool 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.
Enumeration Types
When applied to operands of the sameenum 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.
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.
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.
Master C# with Deep Grasping Methodology!Learn More





