C sharp: Finalize() ~ instead of implementing IDisposable?

If I'm creating a class with network or database connections, why wouldn't I just clean them up inside a destrucotr ~SomeClass Finalize() method instead of having to be bothered with IDisposable and  Dispose()?

I've looked around a bit, but nowhere can I end up actually finding an answer to this simple question:
In C#, If I clean everything up in ~, isn't that acceptable?  Aren't I doing my duty?

Sorry to assign a high/urgent point value to this, but I'm trying to write something fairly quickly and need to sort this out.

coder1313514512456Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

coder1313514512456Author Commented:

The Title of this question is supposed to be:
"C#:  Finalize() ~ instead of implementing IDisposable?"

Not sure why it's being truncated by the number/pound/sharp sign and don't know how to change the Title.



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
coder1313514512456Author Commented:
Thanks saragani.  Apparently I didn't try hard enough.  (And of course now I feel like a complete idiot!)
0
coder1313514512456Author Commented:
Yeah, that would be pretty much exactly what I was looking for.
But here's a question for you:  you would never have an object call Dispose() on itself, right?  Or are there cases?
 
0
saraganiCommented:
I don't understand the question. What exactly do you mean?

I'm not sure if that replies your question, but I for example never write a destructor (I was alway told to avoid ~ ) simple because when the object finally reaches the time when the GC is "killing" it, the destructor starts doing some stuff, preventing the object from being collected by the GC when it was intended to be collected.
(I think that in the links that I gave you they talk about it and say that it will take the GC to cycle to collect that object).

I usually don't have a Dispose function in my object. This is because I usually don't have special things that need to be closed or disposed.
However, there are small exceptions on that matter, for example when an object opens a file and closes it only when the object needs to die or sometimes when working with GDI.

Dispose was made for letting you close some handles and release objects when you think that you finished using that object (And you don't wanna wait for the GC to do that).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.