Delayed instantiation with c# using statment

Posted by fearofawhackplanet on Stack Overflow See other posts from Stack Overflow or by fearofawhackplanet
Published on 2011-01-10T12:44:56Z Indexed on 2011/01/10 12:53 UTC
Read the original article Hit count: 200

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about idisposable