• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

Question on using statement

What are the two places of using "using". I know one of them:

Using <namespace>

What's the other place?
0
chuang4630
Asked:
chuang4630
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
See http://msdn2.microsoft.com/en-us/library/yh598w02.aspx

"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.

0
 
bruintjeCommented:
Hi chuang4630,

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

and
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: http://blogs.labtech.epitech.net/blogs/jaylee/archive/2004/08/16/844.aspx

hope this helps a bit
bruintje
0
 
Fahad MukhtarDistinguished EngineerCommented:
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:
http://msdn2.microsoft.com/en-US/library/sf0df423.aspx

It can also be used as a *STATEMENT*, when it defines a scope at the end of which an object will be disposed. See:
http://msdn2.microsoft.com/en-US/library/yh598w02.aspx
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now