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
266 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
6 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 500 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now