This article about the IDisposable pattern is the continuation of my previous article “Object LifeTime in .NET Framework".
IDisposable is an interface that contains a single method, Dispose(), for releasing unmanaged resources, like files, streams, database connections and so on. This method is implemented explicitly in the code when we need to clean up a disposable object and to release unmanaged resources that this disposable object holds.
The following is an implementation of IDisposable in a class:
There are few things to notice in the code:
- ~DisposableClass()
We can identify it is a destructor method by the symbol ~ (tilde). It is actually converted into a Finalize method because in C# the compiler translates the destructor to a Finalize() method. Please see the following figure showing the Finalize method in the ildasm.exe tool for the destructor ~DisposableClass (that is defined in the preceding sample code).
The question would be: Why we are using the Finalize method if we are already using the Dispose method in the code to clean up managed resources?
The answer is: The Dispose method is implemented explicitly by the programmer and there is the chance that the programmer fails or forgets to dispose of the unmanaged resources, so the Finalize method should be called for a safer side to prevent a memory leak. If the Dispose method is not called for any reason then the destructor would be called by the garbage collector, but when the Finalize method would be called by the garbage collector is not determined. The garbage collector can call it anytime there is a requirement of more memory or when the program exits or anytime on some other conditions.
- GC.SuppressFinalize()
If the Dispose method is called then there is no need of the Object. The Finalize() override method is to be called by the garbage collector and that Dispose() that is implemented explicitly in the code would clean up all the unmanaged resources. That is why the GC. SuppressFinalize() method is called to prevent the garbage collector from running the finalizer.
Both the Finalize and Dispose methods are used to destroy unmanaged resources. Finalize has performance issues since it is called by the garbage collector and as I said earlier, when the Finalize method will be called by the garbage collector is not specified.
The Dispose method is automatically called when a using statement is used. All the objects that can implement the IDisposable interface can implement the using statement.
You can use the ildasm.exe tool to check how the Dispose method is called internally when you use a using statement.- using (TextWriter txt= File.CreateText (“myfile.txt”)
- {
- txt.WriteLine (“Comments Started :”);
- }
I hope this article will be helpful for you. Your feedback and suggestions are always welcomed.

Madan ShekarPosted Apr 4, 2019, 5:44 AM
Nice explained GC in your blog ... thanks for sharing valuable info
Mayzar HlaingPosted Jun 25, 2018, 10:32 PM
Thanks you!
SIWACH ARUNPosted Aug 24, 2015, 4:15 AM
God work mam
Abhishek MalakarPosted Aug 18, 2015, 5:24 PM
Very good information, keep posted. :)
Santhakumar MunuswamyPosted Aug 18, 2015, 2:36 PM
Good one
Narasimha Reddy ChennupalliPosted Aug 18, 2015, 3:08 AM
Goo One.... Thanks for sharing.
Vaikesh K PPosted Aug 18, 2015, 1:33 AM
Good
Karthikeyan KPosted Aug 17, 2015, 11:29 PM
Good one...
Upendra Pratap ShahiPosted Aug 17, 2015, 12:55 PM
good.
Saillesh PawarPosted Aug 17, 2015, 12:22 PM
Can you please elaborate using a Data access objects of System.data or Class objects? i want to add on to your article we can use using keyword for automatically disposing the element when they go out of scope.
Praveen KumarPosted Aug 17, 2015, 9:46 AM
Good
Pankaj Kumar ChoudharyPosted Aug 17, 2015, 9:41 AM
Nice work mam........
Gopi ChandPosted Aug 17, 2015, 9:17 AM
Well done
Mohammed IbrahimPosted Aug 17, 2015, 9:10 AM
nice
Nilesh JadavPosted Aug 17, 2015, 8:53 AM
This is amazing mam :)
Sibeesh VenuPosted Aug 17, 2015, 8:48 AM
Nice Share
Rajeesh MenothPosted Aug 17, 2015, 8:28 AM
Good One..
Sheshnath KumarPosted Aug 17, 2015, 8:26 AM
Nice article Sanjukta!
Sanjukta PearlPosted Aug 17, 2015, 8:17 AM
Thank you guys...Your feedback are more valuable to me.
Vaikesh K PPosted Aug 17, 2015, 8:14 AM
Good share :)
Karthikeyan KPosted Aug 17, 2015, 8:07 AM
Thanks for sharing...