In fact your destuctor (or finalizer in .NET speak) may not even get executed at all, at procss shutdown the runtime will only wait two seconds for all finalizers to run before it calles TerminateProcess().
This is one of the reasons I wish they had used a syntax other than destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in teh next version intrduces the following syntax IIRC
class Foo
{
~Foo() {} // this generates an implementation of IDisposable
!Foo() {} // This is a finalizer
}
Regards
Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
You're destructor will get executed - just don't when! Destructors in
.NET are "non-deterministic", meaning you can't predict when they will
be executed. This is because they are called by the garbage collector
when it cleans up your object.
In fact, using destructors as your primary mechanism for doing cleanup
is NOT recommended. It's best practice to employ something called the
"IDisposable" pattern:
> This is one of the reasons I wish they had used a syntax other than destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in
teh next version intrduces the following syntax IIRC
> class Foo
> {
> ~Foo() {} // this generates an implementation of IDisposable
> !Foo() {} // This is a finalizer
> }
This is stupid - why not simply
protected void override Finalize(){}
?
Finalizers are used very rarely, why don't they optimize the syntax
verbosenes not in other places?
--
cody
Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Richard Blewett [DevelopMentor] - 07 Dec 2004 23:55 GMT
Because Microsoft don;t trust you to write
base.Finalize();
in your override - thats why they force a different syntax and the compiler emits the call to base.Finalize() on commpilation.
Regards
Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
> This is one of the reasons I wish they had used a syntax other than
destructor for a finalizer in C#. C++/CLI the successor to Managed C++ in
teh next version intrduces the following syntax IIRC
> class Foo
> {
> ~Foo() {} // this generates an implementation of IDisposable
> !Foo() {} // This is a finalizer
> }
This is stupid - why not simply
protected void override Finalize(){}
?
Finalizers are used very rarely, why don't they optimize the syntax
verbosenes not in other places?