# using statement (C# Reference)

The using statement provides a convenient syntax that ensures the correct use of IDisposable objects. The await using statement ensures the correct use of IAsyncDisposable objects. The language supports asynchronous disposable types that implement the System.IAsyncDisposable interface.

## Example

The following example shows how to use the using statement.

string manyLines = @"This is line one
This is line two
Here is line three
The penultimate line is line four
This is the final, fifth line.";

{
string? item;
do
{
Console.WriteLine(item);
} while (item != null);
}


The using declaration doesn't require braces:

string manyLines = @"This is line one
This is line two
Here is line three
The penultimate line is line four
This is the final, fifth line.";

string? item;
do
{
Console.WriteLine(item);
} while (item != null);


## Remarks

File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface, or the IAsyncDisposable interface.

When the lifetime of an IDisposable object is limited to a single method, you should declare and instantiate it in the using statement or using declaration. The using declaration calls the Dispose method on the object in the correct way when it goes out of scope. The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned. A variable declared with a using declaration is read-only. If the object implements IAsyncDisposable instead of IDisposable, either using form calls the DisposeAsync and awaits the returned ValueTask. For more information on IAsyncDisposable, see Implement a DisposeAsync method.

Both using forms ensure that Dispose (or DisposeAsync) is called even if an exception occurs within the using block. You can achieve the same result by putting the object inside a try block and then calling Dispose (or DisposeAsync) in a finally block; in fact, this is how the using statement and the using declaration are translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

string manyLines = @"This is line one
This is line two
Here is line three
The penultimate line is line four
This is the final, fifth line.";

{
try
{
string? item;
do
{
Console.WriteLine(item);
} while (item != null);
}
finally
{
}
}


The newer using statement syntax translates to similar code. The try block opens where the variable is declared. The finally block is added at the close of the enclosing block, typically at the end of a method.

For more information about the try-finally statement, see the try-finally article.

Multiple instances of a type can be declared in a single using statement, as shown in the following example. Notice that you can't use implicitly typed variables (var) when you declare multiple variables in a single statement:

string numbers = @"One
Two
Three
Four.";
string letters = @"A
B
C
D.";

right = new StringReader(letters))
{
string? item;
do
{
Console.Write(item);
Console.Write("    ");
Console.WriteLine(item);
} while (item != null);
}


You can combine multiple declarations of the same type using the declaration syntax as well, as shown in the following example:

string numbers = @"One
Two
Three
Four.";
string letters = @"A
B
C
D.";

right = new StringReader(letters);
string? item;
do
{
Console.Write(item);
Console.Write("    ");
Console.WriteLine(item);
} while (item != null);


You can instantiate the resource object and then pass the variable to the using statement, but this isn't a best practice. In this case, after control leaves the using block, the object remains in scope but probably has no access to its unmanaged resources. In other words, it's not fully initialized anymore. If you try to use the object outside the using block, you risk causing an exception to be thrown. For this reason, it's better to instantiate the object in the using statement and limit its scope to the using block.

string manyLines = @"This is line one
This is line two
Here is line three
The penultimate line is line four
This is the final, fifth line.";

{
string? item;
do
{

For more information about disposing of IDisposable objects, see Using objects that implement IDisposable.