Search code examples
c#dispose

Implementing Dispose() with class derived from System.ComponentModel.Component


I made a class that derives from Component:

public class MyComponent: System.ComponentModel.Component
{


}

I saw that Visual Studio put this code in for me:

protected override void Dispose(bool disposing)
{
    try
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        base.Dispose(disposing);
    }
}

MyComponent has a member that is a DataSet and maybe there's some other members that implement IDisposable. What, if anything, do i need to modify with the Dispose() method to make sure things are cleaned up properly? Thanks for helping.


Solution

  • Change:

    if (disposing && (components != null))
    {
         components.Dispose();
    }
    

    to be:

    if (disposing && (components != null))
    {
        _dataset.Dispose();
        components.Dispose();
    }