How can you force Dispose method to be called automatically in .NET?Sometimes
you might forget to call the Dispose method in your code but you might
require it.
Best way is to force this method to be called automatically. This can be achieved in .NET by calling the Dispose method inside Finalize method. Also, to ensure that the garbage collector doesnt run two times, you should include GC.SuppressFinalize method call inside Dispose method. Here is a sample code in C# to demonstrate it: public class
sampleClass:IDisposable { In this example, you release the unmanaged resource namely sampleReader from inside Dispose method and you call Dispose method from inside the destructor. This is because you cannot override Finalize method directly in C#. In VB.NET, you will be calling Dispose method from inside Finalize method as shown below: Overrides
Sub Finalize()
|