I understand roughly how the Garbage Collector works. And I know that destructors are called by the GC. What I need is something that is reliably called upon the event of an object falling out of scope.
One thought is this: An object's ref count drops to zero when it falls out of scope. Is there an event or other mechanism that can be tapped into when the ref count drops to zero?
Remember, I'm not using this like a destructor to free up resources, I need it for timing, synchronization, etc. So it has to happen just like a C++ destructor. I don't care what this mechanism is called, or how it's used, and I know it must exist or the Ref Count could never drop to zero.
Main Topics
Browse All Topics





by: Timbo87Posted on 2004-11-15 at 13:45:35ID: 12588300
Destructors are possible in C#, although they can't be explicitly called (I believe they can be in C++, but I don't do much development in it). They are syntacticly identical to C++.
the A destructor.");
om/Code/20 02/June/ Un derstandin gDestructo rs.asp
public class A
{
~A()
{
Console.WriteLine("Running
}
}
Under the hood, this is translated to override the Finalize method of the base class Object.
There's an in-depth article which goes into some of the IL here:
http://www.c-sharpcorner.c