using Statement (C# Reference)
This page is specific to:.NET Framework Version:1.12.03.03.54.0
C# Language Reference
using Statement (C# Reference)

Defines a scope, outside of which an object or objects will be disposed.

Syntax

        using (Font font1 = new Font("Arial", 10.0f))
{
}
Remarks

C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.

The object can be declared in the using statement, as shown above, or before the using statement, like this:

Font font2 = new Font("Arial", 10.0f);
using (font2)
{
    // use font2
}

Multiple objects can be used in with a using statement, but they must be declared inside the using statement, like this:

        using (Font font3 = new Font("Arial", 10.0f), 
           font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}
Example

The following sample shows how a user-defined class can implement its own Dispose behavior. Note that your type must inherit from IDisposable.

using System;

class C : IDisposable
{
    public void UseLimitedResource()
    {
        Console.WriteLine("Using limited resource...");
    }

    void IDisposable.Dispose()
    {
        Console.WriteLine("Disposing limited resource.");
    }
}

class Program
{
    static void Main()
    {
        using (C c = new C())
        {
            c.UseLimitedResource();
        }
        Console.WriteLine("Now outside using statement.");
        Console.ReadLine();
    }
}
C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 5.3.3.17 Using statements

  • 8.13 The using statement

See Also

Reference

C# Keywords
extern (C# Reference)
Namespace Keywords (C# Reference)
using Directive (C# Reference)
Implementing Finalize and Dispose to Clean Up Unmanaged Resources

Concepts

C# Programming Guide
Namespaces (C# Programming Guide)

Other Resources

C# Reference

Community Content

More Information on Using Statements
Added by:Will Sullivan
See http://msdnwiki.microsoft.com/en-us/mtpswiki/3bwa4xa9(vs.80).aspx for information and examples of stacking using statements.
Prevent indentation by combining multiple using-statements
Added by:Kristof Verbiest
You can combine multiple using-statements, which prevents unnecessary indentation. For more information, see here: http://kristofverbiest.blogspot.com/2008/09/prevent-indentation-by-combining-using.html
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View