Difference between Dispose() and Close()

VT_Bassman

New member
Joined
Apr 21, 2011
Messages
2
Programming Experience
10+
I used to always do the following:
con.close()
set con = nothing

Does con.Dispose() do both of the above 2 lines of code or do I do con.close() and then con.dispose()?
 
In some cases where is it natural to 'close' a disposable object, for example close a connection, the class includes a Close method that calls Dispose method, so in those cases Dispose and Close does the same thing. This kind of close behaviour is always documented.

Setting a variable to Nothing, thus dereference the object, is only need if the variable persist (for a longer while) after object is disposed, to allow garbage collection to destroy the object and release the memory.
 
No, it do clean up code internal to the object to release its resources, mark the object as disposed (for application usage), and mark object to suppress finalizer if there is one (for GC usage).

When there are no references left to an object the garbage collector (GC) can pick it up. At this point the memory for the object is normally simply released. If the object has a finalizer (destructor), and is not disposed (thus has not suppressed finalization), GC queues the object for finalization, finalize it and leaves it for next collection (which now object being 'old' it can take long while before being released). Objects implement finalizers only if they use unmanaged resources according to the disposable pattern, to ensure these resources are freed even if object disposal is neglected.
 
Back
Top