Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

The using Statement

Updated: there is no inner try...catch in the outer finally block, readers who mentioned it are right! Thanks!

Together with the lock statement, the using keyword is also relatively mysterious:

using (IDisposable instance = ...)

{

    //do something

}

 

is equal to: 

 

IDisposable instance = ...;

try

{

    //do something

}

finally

{

    if (instance != null)

    {

        instance.Dispose();

    }

}

Posted: Oct 25 2008, 07:48 PM by Ricardo Peres | with 2 comment(s)
Filed under:

Comments

Peter said:

Are you sure about that try catch about Dispose?

# October 25, 2008 7:57 PM

Joe Chung said:

There shouldn't be a try-catch in the finally, just a Dispose guarded by a null check.

# October 26, 2008 12:16 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)