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?
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.
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.
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 .
IDisposable is an interface that contains a single method, Dispose(), for releasing unmanaged resources, like files, streams, database connections and so on.
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With