3

EDIT: To eager editors, please read the FULL question In addition, since this question is not only about disposing.

So far I've seen this:

protected override Dispose(bool disposing)
{
    base.Dispose(disposing);
    if (disposing)
       c.Dispose()
}

and this:

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            // Dispose managed resources.
        }

        // There are no unmanaged resources to release, but
        // if we add them, they need to be released here.
    }
    disposed = true;

    // If it is available, make the call to the
    // base class's Dispose(Boolean) method
    base.Dispose(disposing);
}

And Microsoft says CA2215: Dispose methods should call base class dispose, here. In addition, since this question is not only about disposing, here is another example from Microsoft calling base at the last line.

Which one is the correct/most common/better if any?

Community
  • 1
  • 1
Esteban
  • 3,108
  • 3
  • 32
  • 51

2 Answers2

2

It's all about sequence of calls or control-flow, if you wish.

In first example dispose of base class base.Dispose() is called for first and after executed the code of the class itself. In second case, instead, vice versa.

So both of them are correct from behavior point of view, and you have to pick that one which fits best your current requirement, it can vary in the same program in regard of type naturally.

Tigran
  • 61,654
  • 8
  • 86
  • 123
2

Your second snippet is doubtful, depending on whether disposed is a protected field from the base class or not.

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            // Dispose managed resources.
        }    
    }
    disposed = true;    
    base.Dispose(disposing);   // wrong if base.Disposing() depends on disposed
}

The issues to consider are exceptions and dependencies between base and derived class. So use a try/finally and put the base call last. The most general pattern would look like:

protected virtual void Dispose(bool disposing)
{     
  if (!disposed)
  {
     if (disposing)
     {
        // Dispose managed resources.
     }            
  }           
  base.Dispose(disposing);       
  disposed = true;   // inherited or local field 
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • Thank you so much! Could you please provide me an example link, or should I assume that `disposed` is a private class field? – Esteban Sep 29 '13 at 14:03
  • 1
    In the [official pattern](http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx), `disposed` is private. But variations do exist. – H H Sep 29 '13 at 14:07
  • I wouldnt worry too much about the try finally. If the disposer throws an exception then something is deeply wrong. Is the right thing to do with a deeply messed up object to keep on running code in it? – Eric Lippert Sep 29 '13 at 14:48
  • I agree that try/finally is not (should not be) a common practice in Dispose. – H H Sep 29 '13 at 14:58
  • @eric, there are numerous examples where dispose can throw in "normal" use and it is not always possible to avoid. E.g. WCF channels which can only be avoided with an awkward pattern. The question is how many different dispose patterns we need. – adrianm Sep 30 '13 at 05:37