I'm dealing with an C# windows form application and I need to run class destructor immediately right after destroying my object .
using System;
namespace Test
{
class TTTest
{
public TTTest()
{
Console.WriteLine("Constructor Called");
}
~TTTest()
{
Console.WriteLine("Destroyed called");
}
}
class Program
{
static void Main(string[] args)
{
TTTest obj1 = new TTTest();
TTTest obj2 = new TTTest();
obj1 = null;
obj2 = null;
Console.ReadKey();
}
}
}
If I run this code ... The result will be :
Constructor Called
Constructor Called
and wont be :
Constructor Called
Constructor Called
Destroyed called
Destroyed called
I want to force compiler to run Garbage collector and I can do that by :
GC.Collect
But I want to run Destructor of each class right after destroying each object without calling GC.Collect() in my routines automatically.
Is there any way ?
I need a good way to destroyed any things right after destroying my object , and do not repeat GC.Collect() again and again .
Thanks for any help.