No distructor in C# :(

  • Thread starter Thread starter ckkwan
  • Start date Start date
C

ckkwan

Hi C# gurus,

In MFC, we have a CSingleLock class, which will ensure that a mutex is
unlocked by itself when the CSingleLock object goes out of scope (when
the distructor is been invoked).

Yes, in C# we can do that using lock {}

But my question is, if I need to do something similar, where I need to
ensure that when my function goes out of scope something is invoked
automatically (regardless of whether I return or an unexpected
exception been thrown). How can I do that in C#?

TIA.
 
YI think you need to implement the IDispose interface and instanciate
your class with the key word "using"
 
Hi C# gurus,

In MFC, we have a CSingleLock class, which will ensure that a mutex is
unlocked by itself when the CSingleLock object goes out of scope (when
the distructor is been invoked).

Yes, in C# we can do that using lock {}

But my question is, if I need to do something similar, where I need to
ensure that when my function goes out of scope something is invoked
automatically (regardless of whether I return or an unexpected
exception been thrown). How can I do that in C#?

Check out IDisposable in .NET and also the using keyword in C#.
And there are no Destructors, only Finalizers.

Don't think mfc when you are working with (against? *s*) a garbage
collector.
You must think in terms of outer loop, and make sure you dispose inwards.

Happy .Dispose()
- Michael S
 
Hi C# gurus,
In MFC, we have a CSingleLock class, which will ensure that a mutex is
unlocked by itself when the CSingleLock object goes out of scope (when
the distructor is been invoked).

Yes, in C# we can do that using lock {}

But my question is, if I need to do something similar, where I need to
ensure that when my function goes out of scope something is invoked
automatically (regardless of whether I return or an unexpected
exception been thrown). How can I do that in C#?

TIA.

You can use try/finally:
try { .. some code that needs cleanup .. }
finally { .. clean up! ... }

the "finally" block is guaranteed to run whenever and however you exit
the "try" block.

For classes, have a look at the IDisposable interface.

And (as a reaction to the title), C# does have destuctors. Declare as
~Classname. Use only if you need to clean up unmanaged resources.


Hans Kesting
 
Hans said:
And (as a reaction to the title), C# does have destuctors. Declare as
~Classname. Use only if you need to clean up unmanaged resources.

Not as of 2.0 - in C# 2.0 they've been renamed to finalizers to
emphasise the difference between C++ destructors (which are
deterministic) and C#/.NET finalizers (which aren't).

But you're right - only use them for unmanaged resources, and implement
IDisposable too :)

Jon
 
Marc Gravell said:
In addition to the other posts, you might benefit from looking at Jon's
page:

http://www.yoda.arachsys.com/csharp/miscutil/usage/locking.html

This appears to cover much of what you are trying to do, and more.

Marc

I agree. With the addition that you should read the entire website.
Mr. Skeets articles are great.

I wonder how he got all this time to help people.

Did he invest in Enron and got out at the right time?
Does he hide at a university and are still working on his doctors? - Soon,
professor, really soon now....
Or do he run his own company, where he call himself 'development director'
and hardly show up for work?

My guess is all of the above.

- Michael S
 
Michael S said:
I agree. With the addition that you should read the entire website.
Mr. Skeets articles are great.

I wonder how he got all this time to help people.

Did he invest in Enron and got out at the right time?
Does he hide at a university and are still working on his doctors? - Soon,
professor, really soon now....
Or do he run his own company, where he call himself 'development director'
and hardly show up for work?

My guess is all of the above.

- Michael S


No no, you got it all wrong. Jon Skeet is a professional name given to a
group of aliens that work from outer space. They are the backbone of .Net
development and work non-stop, 24 hours a day, 8 days a week (yes, eight -
the mythilogical day most people sleep through and don't realize exists).
Parts of this group spend all day every day working on nothing but articles
while others browse through these newsgroups looking for specific topics
that will enable them to learn more about it's members so that they may one
day come down from their "spaceship" to conquer and colonize the Earth.
You didn't know that?

:P

Mythran
 
Michael said:

ECMA 334 2nd edition section 8.7.9 is labelled "Destructors".

ECMA 334 3rd edition renamed it to finalizers and explained:

<quote>
[Note: In the previous version of this standard, what is now referred to
as a "finalizer" was called a
"destructor". Experience has shown that the term "destructor" caused
confusion and often resulted to
incorrect expectations, especially to programmers knowing C++. In C++, a
destructor is called in a
determinate manner, whereas, in C#, a finalizer is not. To get
determinate behavior from C#, one should use
Dispose. end note]
</quote>

so ...

Arne
 

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

Back
Top