What is the difference, if there is one, between a destructor and a Finalize method in a class?
I recently discovered that Visual Studio 2008 considers a destructor synonymous with a Finalize method, meaning that Visual Studio won't let you simultaneously define both methods in a class.
For example, the following code fragment:
class TestFinalize
{
~TestFinalize()
{
Finalize();
}
public bool Finalize()
{
return true;
}
}
Gives the following error on the call to Finalize in the destructor:
The call is ambiguous between the following methods or properties: 'TestFinalize.~TestFinalize()' and 'TestFinalize.Finalize()'
And if the call to Finalize is commented out, it gives the following error:
Type 'ManagementConcepts.Service.TestFinalize' already defines a member called 'Finalize' with the same parameter types
A destructor in C# overrides System.Object.Finalize
method. You have to use destructor syntax to do so. Manually overriding Finalize
will give you an error message.
Basically what you are trying to do with your Finalize
method declaration is hiding the method of the base class. It will cause the compiler to issue a warning which can be silenced using the new
modifier (if it was going to work). The important thing to note here is that you can't both override
and declare a new
member with identical name at the same time so having both a destructor and a Finalize
method will result in an error (but you can, although not recommended, declare a public new void Finalize()
method if you're not declaring a destructor).