Finalization
In C#, there is no direct way of destroying an object. The nearest thing we have is finalization. A finalizer in C# is the C# equivalent of a destructor in C++. Except in C#, you have no control over if and when it will run this down to the garbage collector to make that decision.
Note
The terms finalizer and destructor are used interchangeably in C#. A finalizer is where the user-defined finalizer code is run. After the finalizer in an object is run, it is once again considered alive and the garbage collector will then finally collect the object. This means an object is actually marked “collectable
” twice if it has a finalizer defined.
Finalization is used by an object to release resources and perform other housekeeping operations prior to the object being garbage-collected. Cleanup operations to release unmanaged resources held by an object can be performed by overriding the protected Finalize()
method.
You have to override the Finalize()
method...