Question on using statement

Posted on 2006-05-07
Last Modified: 2010-04-16
What are the two places of using "using". I know one of them:

Using <namespace>

What's the other place?
Question by:chuang4630
    LVL 85

    Assisted Solution

    by:Mike Tomlinson

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

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

    The "font1" object will be automatically garbage collected when the using block is exited.

    LVL 44

    Assisted Solution

    Hi chuang4630,

    you can use it for aliasing
    using cfg = System.Configuration.ConfigurationSettings;

    It can be used to to wrap a disposable (implements IDisposable) object in a scope so that the Dispose() method is called automatically when exiting the scope.

    a sample of that last use can be seen here source:

    hope this helps a bit
    LVL 19

    Accepted Solution

    The using keyword has two major uses:

    It can be use as a *DIRECTIVE*, when it is used to create an alias for a namespace or to import types defined in other namespaces. See:

    It can also be used as a *STATEMENT*, when it defines a scope at the end of which an object will be disposed. See:

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now