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 is the compound assignment operator for the bitwise inclusive OR (or logical inclusive OR) operation. It evaluates the inclusive OR of its left and right operands, assigns the resulting value to the left operand, and returns that new value.
The expression:
T is the type of x, with the strict exception that the left operand x is evaluated only once. The explicit cast (T) is a fundamental part of the C# specification because bitwise operations on integral types smaller than int (such as byte or short) implicitly promote their operands to int and return an int.
Operand Compatibility and Behavior
The|= operator behaves differently depending on the data types of the operands:
1. Integral Numeric Types (int, long, byte, etc.) and char
The operator performs a bitwise inclusive OR operation on the binary representation of the operands. For each bit position, the resulting bit is 1 if the corresponding bit in either or both operands is 1. It is 0 only if both corresponding bits are 0.
bool)
When applied to bool operands, |= performs a logical inclusive OR operation. The left operand evaluates to true if either the left or the right operand evaluates to true.
Unlike the conditional OR operator (||), the | operation does not short-circuit. Both the left and right operands are fully evaluated before the assignment occurs.
T?)
For nullable value types, the |= operator utilizes lifted operators. For numeric and enum types, if either operand is null, the result of the | operation is null.
However, for the nullable boolean type (bool?), the | operator implements three-valued logic:
true | nullevaluates totrue.false | nullevaluates tonull.null | nullevaluates tonull.
enum)
For enum types, the |= operator performs the bitwise OR operation on the underlying integral type of the enumeration.
Evaluation Order
In the expressionx |= y, the evaluation sequence is strictly defined to handle complex left-hand side expressions (such as properties or indexers):
- The left operand
xis evaluated to determine the target variable, property access, or indexer access. - The right operand
yis evaluated. - The value of
xis fetched. Ifxis a property or indexer, itsgetaccessor is invoked. - The
|operation is performed on the values. - The result is assigned back to
x. Ifxis a property or indexer, itssetaccessor is invoked.
Operator Overloading
C# explicitly forbids overloading compound assignment operators directly. A user-defined type (aclass or struct) cannot overload the |= operator.
However, if a user-defined type overloads the binary | operator, the |= operator is automatically and implicitly overloaded.
Master C# with Deep Grasping Methodology!Learn More





