I know destructor are called by Garbage Collector when object is no longer used. But I want to know
How to call destructor through c# code?
If possible please give some basic example for understanding.
I know destructor are called by Garbage Collector when object is no longer used. But I want to know
How to call destructor through c# code?
If possible please give some basic example for understanding.
You don't call the destructor in .NET The managed heap is handled by the CLR and the CLR only.
You can however define a destructor to a class, the destructor would be called once the object gets collected by the GC
class Foo
{
public Foo()
{
Console.WriteLine("Constructed");
}
~Foo()
{
Console.WriteLine("Destructed");
}
}
Take notice that the destructor doesn't (and can't) have a public modifier in-front of it, it's sort of an hint that you can't explicitly call the destructor of an object.
You can look at the Destructor Microsoft docs.
You need to declare a function with same name as the class name but with a leading "~" sign.