Re: Dispose/Finalize

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.




.



Relevant Pages

  • Re: Cant find IPC$ using net share,A new virus?
    ... If your application requires a connection to a shared ... resource then you should make this connection by ... running a command such as this one from the Command ... Ok!I refer to IPC$ just because our app will work well when the ipc$ ...
    (microsoft.public.win2000.networking)
  • Re: SLOW
    ... shortcut or connection to a resource which user B owns? ... Like a shortcut to user Bs redirected printer or something like it? ... session is constantly looking for a resource which it can't find. ... MCSE, CCEA, Microsoft MVP - Terminal Server ...
    (microsoft.public.windows.terminal_services)
  • Re: I am a php noob and have a quick question
    ... $link, when a connection is established, is a resource id. ... for you to refer to this particular database connection. ... If you look up the mysql functions you will discover that some of them have ...
    (comp.lang.php)
  • Re: Localization / Globalization / language versions - Where to start?
    ... Encoding and Localization: ... When you want the application support another culture later, ... Translate the resource text into another language, ... type the following command in the VS2005 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Localization / Globalization / language versions - Where to start?
    ... Encoding and Localization: ... When you want the application support another culture later, ... Translate the resource text into another language, ... type the following command in the VS2005 ...
    (microsoft.public.dotnet.languages.csharp)