> ## 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.

# C# Bitwise OR Assignment

The `|=` 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:

```csharp theme={"dark"}
x |= y;
```

is semantically equivalent to:

```csharp theme={"dark"}
x = (T)(x | y);
```

where `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`.

```csharp theme={"dark"}
byte a = 0b_1010_0000; // Decimal: 160
byte b = 0b_0000_1100; // Decimal: 12

a |= b; 

// Evaluation:
//   1010 0000 (a)
// | 0000 1100 (b)
// --------
//   1010 1100 (Result implicitly cast back to byte and assigned to a: Decimal 172)
```

**2. Boolean Types (`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.

```csharp theme={"dark"}
bool x = false;
bool y = true;

x |= y; // x becomes true
```

**3. Nullable Value Types (`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 | null` evaluates to `true`.
* `false | null` evaluates to `null`.
* `null | null` evaluates to `null`.

```csharp theme={"dark"}
bool? flag1 = false;
flag1 |= null; // flag1 becomes null

bool? flag2 = true;
flag2 |= null; // flag2 remains true
```

**4. Enumeration Types (`enum`)**
For `enum` types, the `|=` operator performs the bitwise OR operation on the underlying integral type of the enumeration.

```csharp theme={"dark"}
[Flags]
enum Status { None = 0, Active = 1, Pending = 2 }

Status current = Status.Active;
current |= Status.Pending; 
// current is now Status.Active | Status.Pending (Underlying value: 3)
```

## Evaluation Order

In the expression `x |= y`, the evaluation sequence is strictly defined to handle complex left-hand side expressions (such as properties or indexers):

1. The left operand `x` is evaluated to determine the target variable, property access, or indexer access.
2. The right operand `y` is evaluated.
3. The value of `x` is fetched. If `x` is a property or indexer, its `get` accessor is invoked.
4. The `|` operation is performed on the values.
5. The result is assigned back to `x`. If `x` is a property or indexer, its `set` accessor is invoked.

## Operator Overloading

C# explicitly forbids overloading compound assignment operators directly. A user-defined type (a `class` or `struct`) cannot overload the `|=` operator.

However, if a user-defined type overloads the binary `|` operator, the `|=` operator is automatically and implicitly overloaded.

```csharp theme={"dark"}
public struct BitMask
{
    public int Value { get; }
    public BitMask(int value) => Value = value;

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

// The |= operator is now implicitly supported
BitMask mask1 = new BitMask(1);
BitMask mask2 = new BitMask(2);
mask1 |= mask2; 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
