Delayed instantiation with c# using statment
- by fearofawhackplanet
Is there any way to write a using statement without instantiating the IDisposable immediately?
For example, if I needed to do something like:
using (MyThing thing)
{
if (_config == null)
{
thing = new MyThing();
}
else
{
thing = new MyThing(_config);
}
// do some stuff
} // end of 'using'
Is there an accepted pattern for cases like this? Or am I back to handling the IDisposable explicitly again?