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

The `~` symbol in C# serves two distinct syntactical purposes depending on its context: as the unary bitwise complement operator and as the finalizer declaration syntax.

**1. Bitwise Complement Operator**
As a unary operator, `~` performs a bitwise logical negation on integral numeric types and enumeration (`enum`) types. It evaluates the binary representation of the operand and inverts each bit, converting `0` to `1` and `1` to `0`.

*Technical Mechanics:*

* The operator is natively defined for `int`, `uint`, `long`, `ulong`, `nint`, `nuint`, and all `enum` types.
* When applied to smaller integral types (`byte`, `sbyte`, `short`, `ushort`), the operand undergoes implicit numeric promotion to `int` before the bitwise inversion occurs. The result evaluates to an `int`.
* When applied to an `enum` type, the operation evaluates to the `enum` type itself. Unlike smaller integral types, applying `~` to an `enum` with a smaller underlying type (e.g., an `enum` backed by a `byte` or `short`) returns the `enum` type, not an `int`.
* Because C# utilizes two's complement representation for signed integers, applying `~` to a signed integer `x` mathematically yields `-x - 1`.

```csharp theme={"dark"}
uint a = 0b_0000_1111_0000_1111_0000_1111_0000_1111;
uint b = ~a;
// b evaluates to 0b_1111_0000_1111_0000_1111_0000_1111_0000

int c = 5;      // Binary (32-bit): 0000...0000 0101
int d = ~c;     // Binary (32-bit): 1111...1111 1010 (Decimal: -6)

enum State : byte { None = 0, Active = 1, Idle = 2 }
State s = State.Active;
State inverted = ~s; 
// Evaluates to the State enum type, bypassing the standard promotion to int
```

**2. Finalizer Declaration**
When prefixed to a class name within a type definition, `~` declares a finalizer (historically referred to as a destructor). A finalizer is a specialized method invoked non-deterministically by the .NET Garbage Collector (GC) when processing the finalization queue before reclaiming the object's memory.

*Technical Mechanics:*

* Finalizers can only be defined within `class` or `record class` types. They are invalid in `struct` or `record struct` types.
* The declaration cannot include access modifiers (e.g., `public`, `private`).
* The declaration cannot accept parameters.
* The finalizer cannot be invoked explicitly by application code.
* During compilation, the C# compiler translates the `~ClassName()` syntax into an override of the `System.Object.Finalize()` method, automatically wrapping the body in a `try/finally` block that ensures `base.Finalize()` is called.

```csharp theme={"dark"}
public class ResourceHandler
{
    // Finalizer declaration
    ~ResourceHandler()
    {
        // Cleanup logic executed exclusively by the GC finalizer thread
    }
}
```

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