about Interface IDisposable


Tony Johansson


I have the following question:
You are creating a generic class, and you need to dispose of the generic
How can you do this?

A. Call the Object.Dispose method.
B. Implement the IDisposable interface.
C. Derive the generic class from the IDisposable class.
D. Use constraints to require the generic type to implement the IDisposable

The right answer is D but I don't fully understand why this is the right

Assume I create a generic class called Farm. This Farm can contain Animals
like this.
public class Farm<Animals>

Now to my question if this generic class Farm<Animals> implement
then method Dispose can be called to dispose of the generic objects.

I mean if I want to dispose all of the Animals object I can just call the
dispose method located in
the farm class but if I call the dispose method in the Animal class
the Farm object is still there.

Can anybody make a comment about my discussion.




Peter Morris

You must remember the purpose of IDisposable. If for example you have a
class that uses Windows API calls to create a handle then you would
implement IDisposable to ensure that you class releases that resource when
it is finished with. The Dispose method will be executed either via the
IDisposable method or from your class finalizer, but either way it will be
executed and will release the resource. This is an IDisposable
implementation with a finalizer!

You could additionally implement IDisposable on your Farm class which just
calls Dispose() on each of the animals held within it, in this case the Farm
would not need a finalizer because you don't need to absolutely guarantee it
is called on Farm, only on Animal (which has already been covered above).
In this case the Dispose on Farm is just a convenience.



Peter Morris

"You are creating a generic class, and you need to dispose of the generic

It's a bit of a vague question really because it doesn't say how the class
passed will be used

public class ObjectAndAHandle<T>
public T Value { get; set; }
IntPtr Handle { get; }

You might need to dispose of THIS object *if* it had an unmanaged resource
like a file handle or something, but would have no need to call Dispose on T
unless it happens to implement it.

public class SomeKindOfList<T>
public readonly List<T> List { get; }

No need to dispose of the list, but you *might* want to dispose of the
objects inside, however you cannot possibly know because it is generic!

If the question was something like:
"You want to implement a generic class list which disposes of the objects it
references when it is disposed" then the answer would be both B and D.

In summary, the question is crap :)

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

Similar Threads

Question about IDispose 9
about interface 4
About Interface 5
About Interface 2
IDisposable 3
Unsure about destructor & IDisposable for C# 1
IDisposable 3
IDisposable 4