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
262 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 74

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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

13 Experts available now in Live!

Get 1:1 Help Now