Help me understand Dispose() implementation code from MSDN

Posted by Benny on Stack Overflow See other posts from Stack Overflow or by Benny
Published on 2010-03-08T09:52:32Z Indexed on 2010/03/08 10:06 UTC
Read the original article Hit count: 232

Filed under:
|

the following code is from MSDN: Idisposable pattern

protected virtual void Dispose(bool disposing)
    {
        // If you need thread safety, use a lock around these 
        // operations, as well as in your methods that use the resource.
        if (!_disposed)
        {
            if (disposing) {
                if (_resource != null)
                    _resource.Dispose();
                    Console.WriteLine("Object disposed.");
            }

            // Indicate that the instance has been disposed.
            _resource = null;
            _disposed = true;   
        }
    }

why the following statement:

 _resource = null;  
_disposed = true; 

are not enclosed by if (disposing) statement block?

for me i would probably write like this:

if (disposing) {
       if (_resource != null) {
            _resource.Dispose();
            _resource = null;
            _disposed = true;
           }
         Console.WriteLine("Object disposed.");
   }

anything wrong with my version?

© Stack Overflow or respective owner

Related posts about idisposable

Related posts about c#