Search code examples
.netwinformsidisposable

CommonDialog components should to be disposed?


Using code blocks or Dispose method hasn't used in this MSDN example about dialog boxes.

But why it is not disposed despite it has Dispose method ?


Solution

  • Hmya, that is not crystal clear unfortunately. OpenFileDialog and friends inherit Dispose() from the Component class. Every component must implement Dispose() because the Dispose() method of the form calls it. It just so happens to be that there are several Component derived classes that have a do-nothing Dispose() method. The dialog classes are like that, they are dialogs. They clean up any unmanaged resources when the dialog closes. No additional help is needed.

    This isn't exactly documented well. It's somewhat visible, the MSDN Library shows that the method was inherited, not overridden by the class. Which still doesn't make it obvious that bypassing the inherited Component.Dispose() method is okay (it is). If you are uncomfortable with it, many programmers are, then don't hesitate to wrap it with the using statement. It does no harm to call a Dispose() method that doesn't do anything. Well, not the kind of harm you'd be ever be able to measure.