Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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 34

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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.
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…

609 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