Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IDisposable pattern on Windows Form

Tags:

c#

idisposable

I've read about IDisposable pattern on this article and want to implement it in my windows form application. As we know that in windows form .Designer.cs class there is already Dispose method

private System.ComponentModel.IContainer components = null;

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

and in .cs class I'm using Typed Dataset to read and save the data.

public partial class frmCustomerList
{
    private MyTypedDataSet ds = new MyTypedDataSet();
    ...
}

So, how to implement IDisposable to dispose MyTypedDataSet? If I implement IDisposable in frmCustomerList and implement its interface

public partial class frmCustomerList : IDisposable
{
    private MyTypedDataSet ds = new MyTypedDataSet();
    void Dispose()
    {
       ds.Dispose();
    }
}

what about Dispose(bool disposing) method in .Designer.cs?

like image 996
Willy Avatar asked May 16 '14 00:05

Willy


People also ask

How can we implement IDisposable pattern?

Implement the dispose pattern A Dispose(bool) method that performs the actual cleanup. Either a class derived from SafeHandle that wraps your unmanaged resource (recommended), or an override to the Object. Finalize method. The SafeHandle class provides a finalizer, so you do not have to write one yourself.

What is IDisposable pattern in C# and how do you implement it?

For implementing the IDisposable design pattern, the class which deals with unmanaged objects directly or indirectly should implement the IDisposable interface. And implement the method Dispose declared inside of the IDisposable interface. We do not directly deal with unmanaged objects.

When should you use IDisposable?

You should implement IDisposable when your class holds resources that you want to release when you are finished using them. Show activity on this post. When your class contains unmanaged objects, resources, opened files or database objects, you need to implement IDisposable .

What is the use of IDisposable interface in C#?

IDisposable is an interface that contains a single method, Dispose(), for releasing unmanaged resources, like files, streams, database connections and so on.


Video Answer


1 Answers

If you look in the Designer.cs file and look below the dispose method you will see this

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

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

Only InializeComponent() is warned about not modifing. You can cut (not copy) and paste the protected override void Dispose(bool disposing) out of the designer file and move it in your main code file without worry, just be sure to leave the components.Dispose(); part in as any disposable objects you add through the designer will be put in that collection for disposing.

public partial class frmCustomerList
{
    private MyTypedDataSet ds = new MyTypedDataSet();

    protected override void Dispose(bool disposing)
    {
        ds.Dispose();

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

    //The rest of your frmCustomerList.cs file.
}
like image 181
Scott Chamberlain Avatar answered Sep 23 '22 23:09

Scott Chamberlain