Destructor implicitly calls the Finalize method, they are
technically same. Dispose is available with those object which implements IDisposable
interface.
You may see : href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/destructors"
rel="noreferrer">Destructors C# -
MSDN
readability="5">
The destructor implicitly calls Finalize on the
base class of the
object.
Example
from the same link:
class
Car
{
~Car() // destructor
{
//
cleanup statements...
}
}
Destructor
code is implicitly translated to the following
code:
protected
override void Finalize()
{
try
{
// Cleanup
statements...
}
finally
{
base.Finalize();
}
}
Your
understanding for the Destructor is right:
From
href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/destructors"
rel="noreferrer">MSDN
readability="11">
The programmer has no control
over when the destructor is called
because this is determined by the garbage
collector. The garbage
collector checks for objects
that are no longer being used by the
application. If it considers an object
eligible for destruction, it
calls the destructor (if any) and reclaims the
memory used to store
the object. Destructors are also called when the program
exits. It is
possible to force garbage collection by calling Collect, but
most of
the time, this should be avoided because it may create
performance
issues.
No comments:
Post a Comment