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?
Yes,
Disposewill be called. It’s called as soon as the execution leaves the scope of theusingblock, regardless of what means it took to leave the block, be it the end of execution of the block, areturnstatement, or an exception.As @Noldorin correctly points out, using a
usingblock in code gets compiled intotry/finally, withDisposebeing called in thefinallyblock. For example the following code:effectively becomes:
So, because
finallyis guaranteed to execute after thetryblock has finished execution, regardless of its execution path,Disposeis guaranteed to be called, no matter what.For more information, see this MSDN article.
Addendum:
Just a little caveat to add: because
Disposeis guaranteed to be called, it’s almost always a good idea to ensure thatDisposenever throws an exception when you implementIDisposable. Unfortunately, there are some classes in the core library that do throw in certain circumstances whenDisposeis 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 ifDisposewas called during an exception stack unwind, since the original exception gets swallowed in favor of the new exception generated by theDisposecall. It can be maddeningly frustrating. Or is that frustratingly maddening? One of the two. Maybe both.