Destructors in C#????

L

LP

Hello!
I am moving from VB.NET to C#.
Could someone explain to me what is the purpose of destructor in C# class if
GC destroys the object on its own timing. What's the difference between
destructor such as ~className and IDisposable.Dispose and finalize method.
At what point destructor gets called? Doest it get invoked by GC or by the
client. I thought finalize method gets called by GC right before it destroys
the object, or is it a VB.NET thing. I am confused.

Thank you
 
N

Nicholas Paldino [.NET/C# MVP]

LP,

In reality, this is not a destructor, but a finalizer (the difference is
important). Basically, this is called at an arbitrary point in time after
the object goes out of scope for the app domain it was created in. This
point in time would be whenever the GC runs after the object is eligible for
GC.

You are right about a finalizer and when it gets called. It's just that
the syntax that you are used to for a destructor in C++ is used for the
finalizer in C#.

As for IDispose.Dispose, that should be called explicitly by client code
to indicate it is finished with the class, and that it can dispose of any
expensive resources that are not needed anymore (instead of waiting for GC).

Hope this helps.
 
?

=?ISO-8859-1?Q?Anders_Nor=E5s?=

LP said:
Hello!
Could someone explain to me what is the purpose of destructor in C# class if
GC destroys the object on its own timing.
If present; a destructor is automatically called by the garbage
collector just before the object is destroyed. In VB.NET you can have a
Sub Finalize method which is the same as a C# destructor.
What's the difference between
destructor such as ~className and IDisposable.Dispose and finalize method.
To supplement garbage collection, classes can implement the IDisposable
interface to manage resource. IDisposable.Dispose method should be
called by clients when they're finished using an object. You can use the
implementation of Dispose to release resources. Unlike the destructor,
the Dispose method is not called automatically by the garbage collector.
Clients of a class must explicitly call Dispose when you want to release
resources.
At what point destructor gets called? Doest it get invoked by GC or by the
client. I thought finalize method gets called by GC right before it destroys
the object, or is it a VB.NET thing. I am confused.
The .NET Framework uses a system called reference-tracing garbage
collection that periodically releases unused resources. The CLR
periodically destroys objects when the system determines that such
objects are no longer needed. Objects are released more quickly when
system resources are in short supply, and less frequently otherwise.
Therefore you cannot determine when the destructor is called.

For more information see
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
R

Richard Blewett [DevelopMentor]

A destructor is C#'s way of specifying a finalizer

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hello!
I am moving from VB.NET to C#.
Could someone explain to me what is the purpose of destructor in C# class if
GC destroys the object on its own timing. What's the difference between
destructor such as ~className and IDisposable.Dispose and finalize method.
At what point destructor gets called? Doest it get invoked by GC or by the
client. I thought finalize method gets called by GC right before it destroys
the object, or is it a VB.NET thing. I am confused.

Thank you



--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.7.5 - Release Date: 26/01/2005



[microsoft.public.dotnet.languages.csharp]
 
P

Peter Rilling

In addition, a good finalizer(destructor) will call the Dispose method if
necessary, just in case the developer forgot.

Nicholas Paldino said:
LP,

In reality, this is not a destructor, but a finalizer (the difference is
important). Basically, this is called at an arbitrary point in time after
the object goes out of scope for the app domain it was created in. This
point in time would be whenever the GC runs after the object is eligible for
GC.

You are right about a finalizer and when it gets called. It's just that
the syntax that you are used to for a destructor in C++ is used for the
finalizer in C#.

As for IDispose.Dispose, that should be called explicitly by client code
to indicate it is finished with the class, and that it can dispose of any
expensive resources that are not needed anymore (instead of waiting for GC).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

LP said:
Hello!
I am moving from VB.NET to C#.
Could someone explain to me what is the purpose of destructor in C# class
if
GC destroys the object on its own timing. What's the difference between
destructor such as ~className and IDisposable.Dispose and finalize
method.
At what point destructor gets called? Doest it get invoked by GC or by the
client. I thought finalize method gets called by GC right before it
destroys
the object, or is it a VB.NET thing. I am confused.

Thank you
 
P

Patrick Philippot

Hi,
Could someone explain to me what is the purpose of destructor in C#
class if GC destroys the object on its own timing.

A destructor in C# is just a syntax shortcut. It's the same as
overriding the Finalize method. So it's called when the GC calls
Finalize on your object.
What's the difference between destructor such as ~className
and IDisposable.Dispose and finalize method.

Finalize is called only when the GC decides to garbage collect the
object. This may be a problem if your object uses resources (especially
non managed resources) that should be released as soon as possible. In
that case, you might decide to implement the IDisposable interface in
your class. You will explicitly release those resources in Dispose and
thell the GC that your object doesn't need a call to Finalize
(SuppressFinalize). The code using your class will then explicitly call
Dispose on it before abandoning any reference to the object.

Actually, you could do this in a custom method of your own. But
complying with the IDisposable convention is useful in many cases
because many classes in the FCL detect whether an object implements
IDisposable and will call Dispose automatically when needed.
I thought Finalize method gets called by GC right before it destroys
the object,
Yes.

or is it a VB.NET thing. I am confused.

No. It's a .Net thing.
 
L

LP

Thank you all. I have much better understanding now.
or is it a VB.NET thing. I am confused.

No. It's a .Net thing.

Right, so invoking "Finalize" method is a .NET feature, I am with you.
I meant to ask:
Is it correct to say that "overide Finalize()" is VB.NET syntax where
"~myclass(){}" is C# ?
 
H

Helge Jensen

Peter said:
In addition, a good finalizer(destructor) will call the Dispose method if
necessary, just in case the developer forgot.

I am getting very comfortable with the following idiom:

class Foo: IDisposable {

public void Dispose() {
GC.SuppressFinalize(this);
// Do actual cleanup
}
~Foo() {
// Someone forgot to dispose, lets try and help them
Dispose();
// And give a warning, if run in debug-mode, this will be caught
throw new ApplicationException(
string.Format("Forgotten {0}.Dispose()",
this.GetType().FullName));
}
}

It's much simpler than the Dispose(bool) idiom that i've seen around.

Finalizers/Destructors in C# are called "destructors" by Anders
Hjelsberg in his book on the C# language...
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
In reality, this is not a destructor, but a finalizer (the difference is
important).

<snip>

Note that while I agree that it's not the same as a destructor in C++,
it *is* a destructor as far as the C# specification is concerned. I
personally wish they hadn't called it a destructor, but there we go...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Destructors 1
Destructors are useless? 26
destrucor & GC ? 1
GC Dispose method 3
What about Destructor for Remoting cleaning? 1
destructors 8
destructor not being called 7
Debugger not cleaning up 7

Top