DTOR in C#

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

If I want to write my own DTOR in C# what method will it be? I know DTOR's
are not supported in C# but I want something like DTOR in C++ in my C#
Module. How to accomplish that?

TIA
 
Vai2000,

By DTOR, I assume you mean destructor.

If you want to have specific resources released at a specified time,
then you should implement the IDisposable interface, which indicates that
your class should be disposed of when done. Check out the section of the
..NET framework documentation titled "Implementing Finalize and Dispose to
Clean Up Unmanaged Resources", located at (watch for line wrap):

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

It shows how to implement IDisposable for your base class, and your
derived classes as well.

Hope this helps.
 
You can specify a destructor in the same manner as a constructor, but with a ~ in front of it

You may want to look at the IDisposable interface too

HT

RS
 
Thanks,
I tried putting the ~Class and it worked. Though will try implementing the
way msft shows.

Nicholas Paldino said:
Vai2000,

By DTOR, I assume you mean destructor.

If you want to have specific resources released at a specified time,
then you should implement the IDisposable interface, which indicates that
your class should be disposed of when done. Check out the section of the
.NET framework documentation titled "Implementing Finalize and Dispose to
Clean Up Unmanaged Resources", located at (watch for line wrap):

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

It shows how to implement IDisposable for your base class, and your
derived classes as well.

Hope this helps.


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

Vai2000 said:
If I want to write my own DTOR in C# what method will it be? I know DTOR's
are not supported in C# but I want something like DTOR in C++ in my C#
Module. How to accomplish that?

TIA
 
Vai2000,

Yes, it worked, but this is not the same as a destructor in C++. In
C++, a destructor for a class is called when the class goes out of scope.
In C#, this is not the case. The destructor code will be called when a
garbage collection occurs, considering that your instance is eligible for
garbage collection.

What kind of resources are you looking to get rid of? If all you are
doing is holding references to other managed classes (which don't implement
IDisposable), then there isn't much you have to do, you don't have to
implement a finalizer (which is what ~Class really is).


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

Vai2000 said:
Thanks,
I tried putting the ~Class and it worked. Though will try implementing the
way msft shows.

message news:[email protected]...
Vai2000,

By DTOR, I assume you mean destructor.

If you want to have specific resources released at a specified time,
then you should implement the IDisposable interface, which indicates that
your class should be disposed of when done. Check out the section of the
.NET framework documentation titled "Implementing Finalize and Dispose to
Clean Up Unmanaged Resources", located at (watch for line wrap):
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp
It shows how to implement IDisposable for your base class, and your
derived classes as well.

Hope this helps.


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

Vai2000 said:
If I want to write my own DTOR in C# what method will it be? I know DTOR's
are not supported in C# but I want something like DTOR in C++ in my C#
Module. How to accomplish that?

TIA
 
What is not true - that C# does not have anything called a destructor

Actually, the C# Language Specification says different

Conceptually, a destructor and a finalizer are the same in the fact that they both get called when an object is destroyed. Admittedly they do have different names, and an objects finalizer will not get called until the carbage collector runs but, the two are the last thing to be called when object is destroyed. Of course the major difference between the two is that you never know when the GC will call the finalizer so, as you rightly said and i wholeheartedly agree, it is far better to put your clean up code into an implementation of IDisposable

So, if somebody asks how to create a C# destructor then surely the answer is a destructor/finalizer - with something to point them in the direction of IDisposable (just in case they are not aware of the differences)

RS
 
Hi,

In addition to other guys, be very very careful when you implement
finalizer.
You should implement it only if you need to dispose unmanaged code.
Otherwise you are beter with IDispose interface.
 
dtors in C# are not really dtors: they are not deterministic. that means you
never know when they get called, *if* they get called. anyway, you don't
need dtors except you want to manage native resources (handles,
connections..). you should implement IDisposable to manage native
resources.
 
Back
Top