473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Destructors in C#????

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
Nov 16 '05 #1
8 8529
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]
- mv*@spam.guard.caspershouse.com

"LP" <lp@a.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #2
LP wrote:
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/de...izeDispose.asp

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #3
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]
Nov 16 '05 #4
In addition, a good finalizer(destructor) will call the Dispose method if
necessary, just in case the developer forgot.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#Q**************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"LP" <lp@a.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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


Nov 16 '05 #5
Hi,

LP wrote:
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.

--
Patrick Philippot - Microsoft MVP
MainSoft Consulting Services
www.mainsoft.fr

Nov 16 '05 #6
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# ?

"Patrick Philippot" <pa***************@mainsoft.xx.fr> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl... Hi,

LP wrote:
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.

--
Patrick Philippot - Microsoft MVP
MainSoft Consulting Services
www.mainsoft.fr

Nov 16 '05 #7
Peter Rilling wrote:
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.
In reality, this is not a destructor, but a finalizer (the difference


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

--
Helge
Nov 16 '05 #8
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
3
by: Nuno Barros | last post by:
Cn someone tell me if when i call the destructor of a derivated class, the destructor of the base class is called implicitly? Or shall i call the destructor by myself? Thanks in advance ...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
8
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
3
by: alex.gman | last post by:
If I have code like this int f() { // ... stuff ... g(); if(x > 0) return (x+4); // ... more stuff ... always_call(z); return y; }
6
by: mlw | last post by:
Could someone explain why there is no destructor in Java classes? There are many times you need to be called WHEN an object goes out of scope and not when it will eventally be freed.
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.