using statement (C# Reference)
Provides a convenient syntax that ensures the correct use of IDisposable objects. Beginning in C# 8.0, the using
statement ensures the correct use of IAsyncDisposable objects.
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.";
using (var reader = new StringReader(manyLines))
{
string? item;
do {
item = reader.ReadLine();
Console.WriteLine(item);
} while(item != null);
}
Beginning with C# 8.0, you can use the following alternative syntax for the using
statement that 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.";
using var reader = new StringReader(manyLines);
string? item;
do {
item = reader.ReadLine();
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. The using
statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also 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. If the object implements IAsyncDisposable
instead of IDisposable
, the using
statement calls the DisposeAsync and awaits
the returned ValueTask. For more information on IAsyncDisposable, see Implement a DisposeAsync method.
The using
statement ensures 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 is 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.";
{
var reader = new StringReader(manyLines);
try {
string? item;
do {
item = reader.ReadLine();
Console.WriteLine(item);
} while(item != null);
} finally
{
reader?.Dispose();
}
}
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.";
using (StringReader left = new StringReader(numbers),
right = new StringReader(letters))
{
string? item;
do {
item = left.ReadLine();
Console.Write(item);
Console.Write(" ");
item = right.ReadLine();
Console.WriteLine(item);
} while(item != null);
}
You can combine multiple declarations of the same type using the new syntax introduced with C# 8 as well, as shown in the following example:
string numbers=@"One
Two
Three
Four.";
string letters=@"A
B
C
D.";
using StringReader left = new StringReader(numbers),
right = new StringReader(letters);
string? item;
do {
item = left.ReadLine();
Console.Write(item);
Console.Write(" ");
item = right.ReadLine();
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.";
var reader = new StringReader(manyLines);
using (reader)
{
string? item;
do {
item = reader.ReadLine();
Console.WriteLine(item);
} while(item != null);
}
// reader is in scope here, but has been disposed
For more information about disposing of IDisposable
objects, see Using objects that implement IDisposable.
C# language specification
For more information, see The using statement in the C# Language Specification. The language specification is the definitive source for C# syntax and usage.