I have a simple Account class. When I create an object referenced by "a". But after that I assign null to a. Further I have invoked GC.Collect(), whose job is to clear all abandoned object from heap memory. And since the object earlier referenced by a is not more referenced by any reference variable so it should be cleared and hence it's destructor should have been invoked. But surprisingly I am not getting the desired output.
- Is CLR performing some optimization ?
class Account
{
int AccountNo;
string Name;
decimal Balance;
public Account()
{
Console.WriteLine("account object created");
}
~Account()
{
Console.WriteLine("account object destroyed");
}
}
class ObjectOrientedExample
{
static void Main()
{
Account a = new Account();
a = null;
GC.Collect();
}
}