?
Solved

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.

Posted on 2015-02-01
6
Medium Priority
?
309 Views
Last Modified: 2015-02-17
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...
0
Comment
Question by:nicedone
  • 2
4 Comments
 
LVL 40
ID: 40583158
Simply a stupid question. There could not be a generic way of implementing IDisposable, it is very dependent on what the class uses.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40583272
The GC's job is to clean up managed memory. Basically, managed memory is any memory allocated from within .NET. Unmanaged memory, on the other hand, is not freed by the GC. It is typically allocated either by another, non-managed application that you are calling from .NET, or a device driver, or the system itself.

The reason that we have IDisposable is because even though the GC cannot free unmanaged memory, we should still have a way to free up such memory. IDisposable provides a hook for your code to free up the unmanaged memory that has been allocated for it, using whatever mechanism makes sense to do so. The Dispose method that IDisposable provides is that hook. Similarly, there is a Finalize method which can also be used to free up unmanaged memory. However, it is difficult to code a finalizer correctly. The majority of the time, if you need to free unmanaged memory, then you will do so via a Dispose method. The last I read, Dispose is a method that the programmer would call, and Finalize is a method that the runtime would call.
0
 
LVL 40
ID: 40584112
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.
0
 
LVL 35

Expert Comment

by:it_saige
ID: 40614448
*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-
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question