Link to home
Start Free TrialLog in
Avatar of nicedone
nicedone

asked on

Write a generic base class called GenericDispose<T> that implements the IDisposable add dispose method and properties.T must be reference type and be instantiated.

Hi,

For my brothers exam he asked me a question like that, as i thought i knew c#, i realized that for a question like this i did not have a clue or havent done something before . could you help me understand and write the code for above question please? i thought GC would take care of getting memory relieved without us intervening,is not that true? in which circumstances do we have to implement IDisposable and if we do how should the question above be answered?  Thank you in advance...
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Simply a stupid question. There could not be a generic way of implementing IDisposable, it is very dependent on what the class uses.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As kaufmed explained, the role of the IDisposable interface is to define a way to release unmanaged memory, that is usually COM objects (objects that are not .NET objects).

And a generic class is a class that can be used with different other classes.

Since each class uses different objects, and since each type of object might have a special requisites or uses different ways to free them, then there is in my opinion no for a generic class to properly dispose of objects.

Rereading the original question, I also see something that does not make sense. There are no properties involved when implementing IDisposable. Either this question is a catch, either the teacher does not know much about disposing of objects.
*No Points*

@James - it is really hard to discern what concept the instructor is trying to teach.  Is it Generics???  Is it Interfaces???  Is it GC???  Is it Reflection???

To add a broad concept, you may be able to use reflection to go over T's properties and call the properties dispose method.  Honestly, though, it seems like a lot of work for such a simple task.

-saige-