Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

using stmt usage

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AVL - 24 Sep 2007 10:44 GMT
hi,
i've seen in certain code snippets the following usage of using statement..
using(RegistryKey key = Registry.Users)
{
 //code
}

what exactly is the usage of using stmt in this case?
can anyone explain...
Patrice - 24 Sep 2007 11:57 GMT
Use the F1 key or check the doc :
http://msdn2.microsoft.com/en-us/library/aa664736(VS.71).aspx (813 The using
statement)

It dispose the resource at the end of the block.

--
Patrice

> hi,
> i've seen in certain code snippets the following usage of using
[quoted text clipped - 6 lines]
> what exactly is the usage of using stmt in this case?
> can anyone explain...
Kevin Spencer - 24 Sep 2007 12:01 GMT
The using statement block ensures that the IDisposable class instantiated in
the beginning of the statement is disposed as soon as the block is exited.
It is the equivalent of using try/catch/finally, and calling Dispose on the
IDisposable class instantiated in the try block.

Signature

HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

> hi,
> i've seen in certain code snippets the following usage of using
[quoted text clipped - 6 lines]
> what exactly is the usage of using stmt in this case?
> can anyone explain...
Peter Duniho - 24 Sep 2007 12:38 GMT
> The using statement block ensures that the IDisposable class instantiated in
> the beginning of the statement is disposed as soon as the block is exited.
> It is the equivalent of using try/catch/finally, and calling Dispose on the
> IDisposable class instantiated in the try block.

A very minor point, admittedly, but the above is not strictly correct.
In particular, the object being disposed is not instantiated or
otherwise initialized within the try block.  The only thing that winds
up in the try block is the code that's in the using statement's embedded
statement (the code immediately following the using statement).  The
using statement is equivalent to initializing the variable outside the
try/finally block.

So:

    using (SomeType obj = ...)
    {
        // some code here
    }

Is equivalent to:

    {
        SomeType obj = ...;
        IDisposable objHidden = (IDisposable)obj;

        try
        {
            // some code here
        }
        finally
        {
            if (objHidden != null)
            {
                objHidden.Dispose();
            }
        }
    }

The spec describes it slightly differently, but IMHO the above makes it
a little more clear (at least if I have understood the spec correctly :)
).  Specifically, according to the spec the variable used for the actual
dispose at the end is initialized by the expression in the using
statement ("..." above), is read-only and inaccessible by the program
itself (so no matter what you do to the visible variable in the embedded
statement of the using statement, the original object is the one that
gets disposed).

Some other possibly interesting behaviors implicit in the "using"
statement beyond those illustrated above:

Obviously, SomeType must implement IDisposable, otherwise a compiler
error occurs.

Also, if SomeType isn't a nullable type, then of course the check for
null isn't done in the "finally" block.

Hope that helps more than confuses the issue.  :)

Pete
Kevin Spencer - 25 Sep 2007 10:51 GMT
You're correct, of course, Peter. My time has not been particularly abundant
lately, and I didn't pore over the details of my response sufficiently to
catch that minor point at the end. Your example does a good job of
illustrating the entire concept.

Signature

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

>> The using statement block ensures that the IDisposable class instantiated
>> in the beginning of the statement is disposed as soon as the block is
[quoted text clipped - 54 lines]
>
> Pete
Martin CLAVREUIL - 30 Sep 2007 18:27 GMT
Hi,

Using (object) {} is the block equivalent to
1-object definition (declaration)
2-usage [..]
3-destruction (disposal)

AVL wrote :
> hi,
> i've seen in certain code snippets the following usage of using statement..
[quoted text clipped - 5 lines]
> what exactly is the usage of using stmt in this case?
> can anyone explain...

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.