using(var myDisposable = new MyDisposable)
{
//Do stuff.
}
is great and all. But if what you wanted was:
using(var myDisposable = new MyDisposable)
{
var myAnswer = CalculateMyAnswer(myDisposable);
}
and you wanted to use myAnswer
later, then you have to declare myAnswer
outside the block, and it's starting to get a bit faffy. :(
Is there any way to declare a using
block, but have it not encapsulate scope, so that variables declared inside that block are still visible outside it?
So far my best solution is to just dispose my variable manually:
var myDisposable = new MyDisposable
var myAnswer = CalculateMyAnswer(myDisposable);
myDisposable.Dispose();
but that loses a lot of what I like about using
.
Are there any alternatives?