Re: Dispose/Finalize
- From: "Scott M." <s-mar@xxxxxxxxxxxxx>
- Date: Tue, 15 Jul 2008 16:49:19 -0400
Thanks, but I do understand that portion.
My confusion was about where the line exactly is between managed and
unmanged, particularly when database connections are concerned.
As you point out the close command needs to be sent to the database, but the
object that sends the command is a .NET managed object (unless we're talking
about the Odbc provider), so that makes a .NET connection a managed
resource, rather than an unmanged one.
"Brian Rasmussen [C# MVP]" <brian@xxxxxxxxxxxx> wrote in message
news:4F1CAEB5-736D-4C36-A555-48A7514D1F0D@xxxxxxxxxxxxxxxx
I will try to avoid repeating what you already seem to know about
Dispose/Finalize. The CLR will make sure memory is reclaimed thanks to
garbage collection. However, it knows nothing about any other kind of
resources basically. So for instance if you have a network connection,
where you need to send a specific close command to a server the CLR can't
provide any help. In this case you could (and should) implement IDisposable
to allow for deterministic clean-up. You could also provide a finalizer as
a safe guard (i.e. if the user didn't call Dispose for some reason).
--
Regards,
Brian Rasmussen [C# MVP]
http://kodehoved.dk
"Scott M." <s-mar@xxxxxxxxxxxxx> wrote in message
news:u0zQn443IHA.4800@xxxxxxxxxxxxxxxxxxxxxxx
While I understand what Dispose and Finalize do and the difference
between them, I am still struggling to understand why I might implement
IDisposable in my classes.
If I were to use an resource in one of my classes that needed cleaning
up, wouldn't I just call *that* object's Dispose() method in my methods
when my class is done with it? And, wouldn't that mean that no one using
my class would need to worry about Disposing my class?
Also, can someone explain clearly how to draw the line between a managed
resource and an unmanaged resource? Is a SqlClient.SqlConnection a
managed or unmanged resource? If I was writing my own Dispose/Finalize,
would I put it into the managed or unmanaged cleanup section? How about
a StreamReader? These are managed objects to be sure, but they *hold*
references to unmanaged objects, so how do I classify them?
Thanks.
.
- Follow-Ups:
- Re: Dispose/Finalize
- From: Alvin Bruney [ASP.NET MVP]
- Re: Dispose/Finalize
- References:
- Dispose/Finalize
- From: Scott M.
- Re: Dispose/Finalize
- From: Brian Rasmussen [C# MVP]
- Dispose/Finalize
- Prev by Date: Re: Dispose/Finalize
- Next by Date: Re: Dispose/Finalize
- Previous by thread: Re: Dispose/Finalize
- Next by thread: Re: Dispose/Finalize
- Index(es):
Relevant Pages
|