using System;
public class MyClass : IDisposable
{
~MyClass()
{
Console.WriteLine("In destructor!");
}
public void Dispose()
{
Console.WriteLine("In Dispose() for {0}!");
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();
Console.WriteLine("***** Sweaping gen 0 *****");
GC.Collect(0);
Console.WriteLine("\n***** New generations *****");
Console.WriteLine("C1 is gen {0}", GC.GetGeneration(c1));
Console.WriteLine("C2 is gen {0}", GC.GetGeneration(c2));
Console.WriteLine("C3 is gen {0}", GC.GetGeneration(c3));
Console.WriteLine("C4 is gen {0}", GC.GetGeneration(c4));
}
}
Output
***** Disposing c1 and c3 *****
In Dispose() for {0}!
In Dispose() for {0}!
***** Sweaping gen 0 *****
***** New generations *****
C1 is gen 1
C2 is gen 1
C3 is gen 1
C4 is gen 1
In destructor!
In destructor!