Why destructors and not just Finalize()?
In one of the comments, Niall said:
“I am curious as to why the C# syntax for a finalizer is the destructor syntax. It doesn't bother me one way or another, but I am still curious.”
So I thought I'd devote a bit of time to that question.
Say I'm writing some code that wraps some sort of resource. Here's the code I might write if C# didn't support the destructor syntax (and the class already has implemented the Dispose design pattern:
class MyClass: MyBaseClass
{
IntPtr myResource;
... lots of stuff here...
protected override void Finalize()
{
Dispose(false);
}
protected override void Dispose(bool disposing)
{
// free myResource here...
}
}
That seems pretty straightforward. What's wrong with the code?
The answer is the reason that we have a separate syntax. Please post your answer in the comments.