> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/languages/c-sharp/operators-and-expressions.md).

# Operators and Expressions

## Boolean Logical Operators

* Unary `!` (logical negation) operator.
* Binary `&` (logical `AND`), `|` (logical `OR`), and `^` (logical exclusive `OR`) operators. \
  Those operators always evaluate both operands.
* Binary `&&` (conditional logical `AND`) and `||` (conditional logical `OR`) operators. \
  Those operators evaluate the right-hand operand only if it's necessary.

{% hint style="info" %}
For operands of the **integral numeric types**, the `&`, `|`, and `^` operators perform bitwise logical operations.
{% endhint %}

### Logical negation operator ! <a href="#logical-negation-operator" id="logical-negation-operator"></a>

<pre class="language-csharp"><code class="lang-csharp">bool passed = false;
<strong>Console.WriteLine(!passed);  // output: True
</strong><strong>Console.WriteLine(!true);    // output: False
</strong></code></pre>

### Logical AND operator & <a href="#logical-and-operator" id="logical-and-operator"></a>

<pre class="language-csharp"><code class="lang-csharp">bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

<strong>bool a = false &#x26; SecondOperand();
</strong>Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// False

<strong>bool b = true &#x26; SecondOperand();
</strong>Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
</code></pre>

### Logical OR operator | <a href="#logical-or-operator" id="logical-or-operator"></a>

<pre class="language-csharp"><code class="lang-csharp">bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

<strong>bool a = true | SecondOperand();
</strong>Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// True

<strong>bool b = false | SecondOperand();
</strong>Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
</code></pre>

|   x   |   y   |  x\&y |  x\|y |
| :---: | :---: | :---: | :---: |
|  true |  true |  true |  true |
|  true | false | false |  true |
|  true |  null |  null |  true |
| false |  true | false |  true |
| false | false | false | false |
| false |  null | false |  null |
|  null |  true |  null |  true |
|  null | false | false |  null |
|  null |  null |  null |  null |

### Logical exclusive OR operator ^ <a href="#logical-exclusive-or-operator" id="logical-exclusive-or-operator"></a>

<pre class="language-csharp"><code class="lang-csharp"><strong>Console.WriteLine(false ^ false);  // output: False
</strong><strong>Console.WriteLine(false ^ true);   // output: True
</strong><strong>Console.WriteLine(true ^ false);   // output: True
</strong><strong>Console.WriteLine(true ^ true);    // output: False
</strong></code></pre>

### Conditional logical AND operator && <a href="#conditional-logical-and-operator" id="conditional-logical-and-operator"></a>

```csharp
bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

bool a = false && SecondOperand();
Console.WriteLine(a);
// Output:
// False

bool b = true && SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
```

### Conditional logical OR operator || <a href="#conditional-logical-or-operator" id="conditional-logical-or-operator"></a>

```csharp
bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

bool a = true || SecondOperand();
Console.WriteLine(a);
// Output:
// True

bool b = false || SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True
```

|           Operator Precedence          |
| :------------------------------------: |
| `!` > `&` > `^` > `\|` > `&&` > `\|\|` |

## &#x20;null-coalescing operators (?? and ??=) <a href="#and-operators-the-null-coalescing-operators" id="and-operators-the-null-coalescing-operators"></a>
