I've the following code
using(MemoryStream ms = new MemoryStream())
{
//code
return 0;
}
The dispose()
method is called at the end of using
statement braces }
right? Since I return
before the end of the using
statement, will the MemoryStream
object be disposed properly? What happens here?
Your MemoryStream object will be disposed properly, no need to worry about that.
using
statements behave exactly liketry ... finally
blocks, so will always execute on any code exit paths. However, I believe they are subject to the very few and rare situations in whichfinally
blocks are not called. One example that I can remember is if the foreground thread exits while background threads are active: all threads apart from the GC are paused, meaningfinally
blocks are not run.Obvious edit: they behave the same apart from the logic that lets them handle IDisposable objects, d'oh.
Bonus content: they can be stacked (where types differ):
And also comma-delimited (where types are the same):
Yes,
Dispose
will be called. It's called as soon as the execution leaves the scope of theusing
block, regardless of what means it took to leave the block, be it the end of execution of the block, areturn
statement, or an exception.As @Noldorin correctly points out, using a
using
block in code gets compiled intotry
/finally
, withDispose
being called in thefinally
block. For example the following code:effectively becomes:
So, because
finally
is guaranteed to execute after thetry
block has finished execution, regardless of its execution path,Dispose
is guaranteed to be called, no matter what.For more information, see this MSDN article.
Addendum:
Just a little caveat to add: because
Dispose
is guaranteed to be called, it's almost always a good idea to ensure thatDispose
never throws an exception when you implementIDisposable
. Unfortunately, there are some classes in the core library that do throw in certain circumstances whenDispose
is called -- I'm looking at you, WCF Service Reference / Client Proxy! -- and when that happens it can be very difficult to track down the original exception ifDispose
was called during an exception stack unwind, since the original exception gets swallowed in favor of the new exception generated by theDispose
call. It can be maddeningly frustrating. Or is that frustratingly maddening? One of the two. Maybe both.With the
using
statement, the object will be disposed of regardless of the completion path.Further reading...
Take a look at your code in reflector after you compile it. You'll find that the compiler refactors the code to ensure that dispose is called on the stream.