> ## 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# Logical OR

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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
int value = 4;      // 0b_0100
value |= 2;         // 0b_0010
// value is now 6   // 0b_0110

bool flag = false;
flag |= true;
// flag is now true
```

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