C#idiomatic C#
usingcsharp/language-reference/statements

Language

using statement - ensure the correct use of disposable objects

Use the C# using statement or declaration to ensure the correct use of disposable objects

  • page csharp/language-reference/statements/using
  • updated 01/16/2026

The using statement ensures the correct use of an System.IDisposable instance:

sample · csharpdocs/csharp/language-reference/statements/snippets/using/Program.csUsing

csharp

static void Using()
{
    // <Using>
    var numbers = new List<int>();
    using (StreamReader reader = File.OpenText("numbers.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) is not null)
        {
            if (int.TryParse(line, out int number))
            {
                numbers.Add(number);
            }
        }
    }
    // </Using>
}

static async void AwaitUsing()
{
    // <AwaitUsing>
    await using (var resource = new AsyncDisposableExample())
    {
        // Use the resource
    }
    // </AwaitUsing>
}

// <UsingDeclaration>
static IEnumerable<int> LoadNumbers(string filePath)
{
    using StreamReader reader = File.OpenText(filePath);
    
    var numbers = new List<int>();
    string line;
    while ((line = reader.ReadLine()) is not null)
    {
        if (int.TryParse(line, out int number))
        {
            numbers.Add(number);
        }
    }
    return numbers;
}
// </UsingDeclaration>

static void MultipleResources()
{
    // <MultipleResources>
    using (StreamReader numbersFile = File.OpenText("numbers.txt"), wordsFile = File.OpenText("words.txt"))
    {
        // Process both files
    }
    // </MultipleResources>
}

static IEnumerable<int> UsingWithExpression(string filePath)
{
    var result = new List<int>();
    // <UsingWithExpression>
    StreamReader reader = File.OpenText(filePath);
    
    using (reader)
    {
        // Process file content
    }
    // </UsingWithExpression>
    return result;
}

When control leaves the block of the using statement, the acquired System.IDisposable instance is disposed. In particular, the using statement ensures that a disposable instance is disposed even if an exception occurs within the block of the using statement. In the preceding example, an opened file is closed after all lines are processed.

Use the await using statement to correctly use an System.IAsyncDisposable instance:

sample · csharpdocs/csharp/language-reference/statements/snippets/using/Program.csAwaitUsing

csharp

static void Using()
{
    // <Using>
    var numbers = new List<int>();
    using (StreamReader reader = File.OpenText("numbers.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) is not null)
        {
            if (int.TryParse(line, out int number))
            {
                numbers.Add(number);
            }
        }
    }
    // </Using>
}

static async void AwaitUsing()
{
    // <AwaitUsing>
    await using (var resource = new AsyncDisposableExample())
    {
        // Use the resource
    }
    // </AwaitUsing>
}

// <UsingDeclaration>
static IEnumerable<int> LoadNumbers(string filePath)
{
    using StreamReader reader = File.OpenText(filePath);
    
    var numbers = new List<int>();
    string line;
    while ((line = reader.ReadLine()) is not null)
    {
        if (int.TryParse(line, out int number))
        {
            numbers.Add(number);
        }
    }
    return numbers;
}
// </UsingDeclaration>

static void MultipleResources()
{
    // <MultipleResources>
    using (StreamReader numbersFile = File.OpenText("numbers.txt"), wordsFile = File.OpenText("words.txt"))
    {
        // Process both files
    }
    // </MultipleResources>
}

static IEnumerable<int> UsingWithExpression(string filePath)
{
    var result = new List<int>();
    // <UsingWithExpression>
    StreamReader reader = File.OpenText(filePath);
    
    using (reader)
    {
        // Process file content
    }
    // </UsingWithExpression>
    return result;
}

For more information about using System.IAsyncDisposable instances, see the Using async disposable section of the Implement a DisposeAsync method article.

You can also use a using declaration that doesn't require braces:

sample · csharpdocs/csharp/language-reference/statements/snippets/using/Program.csUsingDeclaration

csharp

static void Using()
{
    // <Using>
    var numbers = new List<int>();
    using (StreamReader reader = File.OpenText("numbers.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) is not null)
        {
            if (int.TryParse(line, out int number))
            {
                numbers.Add(number);
            }
        }
    }
    // </Using>
}

static async void AwaitUsing()
{
    // <AwaitUsing>
    await using (var resource = new AsyncDisposableExample())
    {
        // Use the resource
    }
    // </AwaitUsing>
}

// <UsingDeclaration>
static IEnumerable<int> LoadNumbers(string filePath)
{
    using StreamReader reader = File.OpenText(filePath);
    
    var numbers = new List<int>();
    string line;
    while ((line = reader.ReadLine()) is not null)
    {
        if (int.TryParse(line, out int number))
        {
            numbers.Add(number);
        }
    }
    return numbers;
}
// </UsingDeclaration>

static void MultipleResources()
{
    // <MultipleResources>
    using (StreamReader numbersFile = File.OpenText("numbers.txt"), wordsFile = File.OpenText("words.txt"))
    {
        // Process both files
    }
    // </MultipleResources>
}

static IEnumerable<int> UsingWithExpression(string filePath)
{
    var result = new List<int>();
    // <UsingWithExpression>
    StreamReader reader = File.OpenText(filePath);
    
    using (reader)
    {
        // Process file content
    }
    // </UsingWithExpression>
    return result;
}

When declared in a using declaration, a local variable is disposed at the end of the scope in which it's declared. In the preceding example, disposal happens at the end of a method.


ms.custom: "updateeachrelease"

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

A variable declared by the using statement or declaration is readonly. You can't reassign it or pass it as a ref or out parameter.

You can declare several instances of the same type in one using statement, as the following example shows:

sample · csharpdocs/csharp/language-reference/statements/snippets/using/Program.csMultipleResources

csharp

static void Using()
{
    // <Using>
    var numbers = new List<int>();
    using (StreamReader reader = File.OpenText("numbers.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) is not null)
        {
            if (int.TryParse(line, out int number))
            {
                numbers.Add(number);
            }
        }
    }
    // </Using>
}

static async void AwaitUsing()
{
    // <AwaitUsing>
    await using (var resource = new AsyncDisposableExample())
    {
        // Use the resource
    }
    // </AwaitUsing>
}

// <UsingDeclaration>
static IEnumerable<int> LoadNumbers(string filePath)
{
    using StreamReader reader = File.OpenText(filePath);
    
    var numbers = new List<int>();
    string line;
    while ((line = reader.ReadLine()) is not null)
    {
        if (int.TryParse(line, out int number))
        {
            numbers.Add(number);
        }
    }
    return numbers;
}
// </UsingDeclaration>

static void MultipleResources()
{
    // <MultipleResources>
    using (StreamReader numbersFile = File.OpenText("numbers.txt"), wordsFile = File.OpenText("words.txt"))
    {
        // Process both files
    }
    // </MultipleResources>
}

static IEnumerable<int> UsingWithExpression(string filePath)
{
    var result = new List<int>();
    // <UsingWithExpression>
    StreamReader reader = File.OpenText(filePath);
    
    using (reader)
    {
        // Process file content
    }
    // </UsingWithExpression>
    return result;
}

When you declare several instances in one using statement, they are disposed in reverse order of declaration.

You can also use the using statement and declaration with an instance of a ref struct that fits the disposable pattern. That is, it has an instance Dispose method that's accessible, parameterless, and has a void return type.

A return inside a using block still guarantees disposal. The compiler rewrites it into a try/finally, so the resource’s Dispose is always called before the method actually returns.

The using statement can also be of the following form:

csharp
using (expression)
{
    // ...
}

where expression produces a disposable instance. The following example demonstrates that form:

sample · csharpdocs/csharp/language-reference/statements/snippets/using/Program.csUsingWithExpression

csharp

static void Using()
{
    // <Using>
    var numbers = new List<int>();
    using (StreamReader reader = File.OpenText("numbers.txt"))
    {
        string line;
        while ((line = reader.ReadLine()) is not null)
        {
            if (int.TryParse(line, out int number))
            {
                numbers.Add(number);
            }
        }
    }
    // </Using>
}

static async void AwaitUsing()
{
    // <AwaitUsing>
    await using (var resource = new AsyncDisposableExample())
    {
        // Use the resource
    }
    // </AwaitUsing>
}

// <UsingDeclaration>
static IEnumerable<int> LoadNumbers(string filePath)
{
    using StreamReader reader = File.OpenText(filePath);
    
    var numbers = new List<int>();
    string line;
    while ((line = reader.ReadLine()) is not null)
    {
        if (int.TryParse(line, out int number))
        {
            numbers.Add(number);
        }
    }
    return numbers;
}
// </UsingDeclaration>

static void MultipleResources()
{
    // <MultipleResources>
    using (StreamReader numbersFile = File.OpenText("numbers.txt"), wordsFile = File.OpenText("words.txt"))
    {
        // Process both files
    }
    // </MultipleResources>
}

static IEnumerable<int> UsingWithExpression(string filePath)
{
    var result = new List<int>();
    // <UsingWithExpression>
    StreamReader reader = File.OpenText(filePath);
    
    using (reader)
    {
        // Process file content
    }
    // </UsingWithExpression>
    return result;
}

C# language specification

For more information, see The using statement section of the C# language specification.

See also