views:

209

answers:

5

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?

+22  A: 

Yes, Dispose will be called. It's called as soon as the execution leaves the scope of the using block, regardless of what means it took to leave the block, be it the end of execution of the block, a return statement, or an exception.

As @Noldorin correctly points out, using a using block in code gets compiled into try/finally, with Dispose being called in the finally block. For example the following code:

using(MemoryStream ms = new MemoryStream())
{
     //code
     return 0;
}

effectively becomes:

MemoryStream ms = new MemoryStream();
try
{
    // code
    return 0;
}
finally
{
    ms.Dispose();
}

So, because finally is guaranteed to execute after the try 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 that Dispose never throws an exception when you implement IDisposable. Unfortunately, there are some classes in the core library that do throw in certain circumstances when Dispose 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 if Dispose was called during an exception stack unwind, since the original exception gets swallowed in favor of the new exception generated by the Dispose call. It can be maddeningly frustrating. Or is that frustratingly maddening? One of the two. Maybe both.

Randolpho
I think you'll find it's effectively compiled into a try-finally block with a call to `Dispose` in finally, so it's effectively working off the implementation of `finally`, as you describe.
Noldorin
@Noldorin: exactly. Although I suppose I could be explicit about that. Edit forthcoming....
Randolpho
Simple and effective answer, got my vote. :)
Noldorin
Also note that there are some circumstances in which the finally block is not guaranteed to execute, such as using Environment.FailFast and if a StackOverFlowException occurs.
C.McAtackney
@C.McAtackney: also a good point. Also, IIRC, OutOfMemoryException; basically if you can't catch the exception because it's a critical execution failure, Dispose won't be called. Of course, in such a case the program is guaranteed to crash, along with any memory allocated to it, so in 99.9% of cases it's a non-issue, unless you're doing wonky stuff like writing to a file in your dispose method. Aside from the catastrophic program crash, that is.
Randolpho
+5  A: 

Your MemoryStream object will be disposed properly, no need to worry about that.

Otávio Décio
+4  A: 

With the using statement, the object will be disposed of regardless of the completion path.

Further reading...

RSolberg
+1  A: 

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.

Wil P
+4  A: 

using statements behave exactly like try ... 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 which finally 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, meaning finally 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):

using (SqlConnection conn = new SqlConnection("string"))
using (SqlCommand comm = new SqlCommand("", conn))
{

}

And also comma-delimited (where types are the same):

using (SqlCommand comm = new SqlCommand("", conn), 
       SqlCommand comm2 = new SqlCommand("", conn))
{

}
Adam