> ## 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# Using Declaration

A `using` declaration is a variable declaration preceded by the `using` keyword that instructs the compiler to automatically dispose of the object at the end of the enclosing lexical scope. Introduced in C# 8.0, it provides a syntactic alternative to the traditional `using` statement block, reducing indentation while ensuring deterministic resource cleanup.

## Technical Requirements

To utilize a synchronous `using` declaration, the declared type must be implicitly convertible to the `System.IDisposable` interface. For `ref struct` types, the compiler supports pattern-based synchronous disposal, requiring only an accessible `public void Dispose()` method. As of C# 13, `ref struct` types can also implement interfaces directly, meaning they can explicitly implement `IDisposable` as well.

For asynchronous disposal (`await using`), the type must be implicitly convertible to the `System.IAsyncDisposable` interface, *or* it must satisfy the asynchronous disposable pattern. The pattern requires an accessible `DisposeAsync` method that returns a `ValueTask` or another awaitable type. This pattern-based approach is utilized by types like the `ConfiguredAsyncDisposable` struct returned by `ConfigureAwait(false)`.

Once declared, a `using` variable is implicitly read-only; the compiler prevents reassignment or modification of the reference.

## Syntax and Compiler Translation

**Using Declaration Syntax:**

```csharp theme={"dark"}
{
    using var resource = new DisposableResource();
    // Operations utilizing 'resource'
    
} // Disposal is implicitly invoked here
```

At compile time, the Roslyn compiler lowers the `using` declaration into a standard `try...finally` block. For types implicitly convertible to `IDisposable`, the compiler casts the instance to `IDisposable` before invoking `Dispose()`. When dealing with normal value types (structs), boxing is avoided during this interface call because the compiler emits the `constrained.` IL prefix before the virtual method call.

For types utilizing pattern-based disposal, the compiler emits a direct method call instead of an interface cast.

**Equivalent Lowered Syntax:**

```csharp theme={"dark"}
// Conceptual pseudo-code representing compiler lowering logic.
// Note: For non-nullable value types, the compiler omits the null check 
// at the IL level, as 'resource != null' would be invalid in actual C#.
{
    var resource = new DisposableResource();
    try
    {
        // Operations utilizing 'resource'
    }
    finally
    {
        if (resource != null)
        {
            ((IDisposable)resource).Dispose();
        }
    }
}
```

## Scope and Disposal Order

The lifetime of the object is strictly bound to the lexical scope in which the `using` declaration appears. Disposal occurs immediately when execution exits that scope, whether via normal control flow, a `return` statement, or an unhandled exception.

When multiple `using` declarations exist within the same scope, the compiler guarantees that the objects are disposed in reverse order of their declaration (Last-In, First-Out, or LIFO).

```csharp theme={"dark"}
{
    using var resource1 = new DisposableResource();
    using var resource2 = new DisposableResource();
    
} // resource2 is disposed, followed by resource1
```

## Asynchronous Disposal

For types implicitly convertible to `IAsyncDisposable` or those satisfying the asynchronous disposable pattern, the `await` modifier is prepended to the declaration. The compiler lowers this into an asynchronous `try...finally` block, invoking `DisposeAsync()` and awaiting the returned awaitable.

```csharp theme={"dark"}
{
    await using var asyncResource = new AsyncDisposableResource();
    // Asynchronous operations
    
} // await asyncResource.DisposeAsync() is implicitly called here
```

## Out Variables and Pattern Matching

A `using` declaration cannot be used directly within an `out` variable declaration or a pattern matching declaration. The variable must be explicitly declared as a standalone statement to establish its scope and lifetime boundaries clearly for the compiler.

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