IDisposable interface
In this chapter you will learn:
Implement IDisposable interface
MyClass
in the code below implements IDisposable
interface.
In the Dispose
method it outputs the debug message to alert
us that method is being called and calls the garbage collector
to do the garbage collection.
using System;/*from j a v a2 s . c o m*/
public class MyClass : IDisposable
{
~MyClass()
{
Console.WriteLine("In destructor");
}
public void Dispose()
{
Console.WriteLine("In Dispose()");
GC.SuppressFinalize(this);
}
}
public class MainClass
{
public static void Main(string[] args)
{
MyClass c1, c2, c3, c4;
c1 = new MyClass();
c2 = new MyClass();
c3 = new MyClass();
c4 = new MyClass();
Console.WriteLine("\n***** Disposing c1 and c3 *****");
c1.Dispose();
c3.Dispose();
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: