Disposing Collections

G

Guest

This whole dispose topic confuses me regarding managed and unmanaged
resources. I think that it was easier when you just cleaned up every object,
which brings me to my problem.

I have some classes that have Hashtables collections defined as class
variables.
In the past, when I was done with the class, I would remove all the items
from the collection and then set the collection to null.

In C# (.NET General), is this necessary anymore? For example, do I need to
implement a dispose method that would remove all the items from the
collection and set the collection to null. Is it enough to just set the
class instance to null and everything inside would be cleaned up?

Thanks in advance.
Greg
 
C

Carlos J. Quintero [.NET MVP]

No, you don´t need to implement the dispose method in that case.

- If a class has a Dispose method, you need to call it when you are done
with it. For example, if the objects that you store in the collection have
the Dispose method and the collection is the only reference to them, you
should dispose them before removing them from the collection.

- You only need to implement a Dispose method in your class when it wraps an
unmaneged resource, such as a Win32 handle, or a managed object that has a
Dispose method that your class don´t call automatically internally.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
G

Guest

Thank you. Is there a way to tell in code if an object implements the
Dispose method without having to try...catch exceptions.
For example
[not meant to be c# code]
if (isdisposable(class1)) class1.dispose()
 
C

Carlos J. Quintero [.NET MVP]

Since disposable objects should implement the IDisposable interface, you can
use: if (obj1 is IDisposable)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
H

Helge Jensen

gmccallum said:
Thank you. Is there a way to tell in code if an object implements the
Dispose method without having to try...catch exceptions.
For example
[not meant to be c# code]
if (isdisposable(class1)) class1.dispose()

IDisposable objects (usually) represents resouces which should be
released at-once when you are done using them. This introduces a concept
of "ownership" of the disposable.

You shouldn't Dispose() just because you are passes an IDisposable. only
the "owner" should do that.

Usually ownership is on the callstack, which fits the "using" idiom very
nicely, Example:

class Foo {
void WriteTo(Stream s) {
s.Write(...);
// just use stream, don't dispose
}
void f() {
using ( Stream s = new File.Open("foo") ) // "own" s
WriteTo(s);
}
}

Another case is where objects have "owned" members, they often become
resources themselves, becoming candidates for disposal:

class Bar: IDisposable {
Stream s;
public Bar() { s = new File.Open("foo"); }
public void Dispose() {
GC.SuppressFinalize(this);
if ( s != null ) {
s.Dispose();
s = null;
}
}
~Bar() {
// This code should not run in well-written programs
Errors.ForgottenDispose(this);
Dispose();
}
}
 

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

Top