Stacking "using" blocks together
I had always been thinking of using the using-block to instantiate multiple disposable objects so I don't have to remember explicitly calling Close() or Dispose() on them:
using (IDisposable one = new DisposableOne(),
IDisposable two = new DisposableTwo()) { // Compile time error}
But unfortunately C# does not support that. So as a work around, one would do these nested using statements to do the trick:
using (IDisposable one = new DisposableOne())
{using (IDisposable two = new DisposableTwo())
{ // Ops. Code is now indented}
}
But now, you have messed up your pretty-looking indentation of your code. Remember back in the acadamia your programming class instructor told you that all your if-else/for/while statements will work without curly brackets? Well, surprise, same case for using blocks:
using (IDisposable one = new DisposableOne())
using (IDisposable two = new DisposableTwo())
{ // This works! }
Time to show off to your pairing friend you can write prettier code than him/her =D
No comments:
Post a Comment