Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Using using

I thought there was a way to use the using statment in C# to manage disposing of more than one IDisposable object?

I thought it was something like...

using (IDisposable obj1 = new DisposableObject1(), IDisposable obj2 = new DisposableObject2())
{
    obj1.DoIt();
    obj2.DoItAgain();
}

GuyIncognito
Friday, August 22, 2003

You can, as long as you are holding multiple references of the same type (as you are in your example). It's just slightly different syntactically than you showed:

using (IDisposable obj1 = new DisposableObject1(), obj2 = new DisposableObject2())
{
    obj1.DoIt();
    obj2.DoItAgain();
}

(Notice the lack of the second IDisposable.)

Brad Wilson (dotnetguy.techieswithcats.com)
Saturday, August 23, 2003

Oh, and of course you could just stack the usings together:

using (Foo foo = new Foo())
  using (Bar bar = new Bar())
  {
  }

Brad Wilson (dotnetguy.techieswithcats.com)
Saturday, August 23, 2003

OK, thanks... I knew I wasn't crazy. 

Guy Incognito
Sunday, August 24, 2003

*  Recent Topics

*  Fog Creek Home