473,414 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

about Interface IDisposable

Hello!

I have the following question:
You are creating a generic class, and you need to dispose of the generic
objects.
How can you do this?

A. Call the Object.Dispose method.
B. Implement the IDisposable interface.
C. Derive the generic class from the IDisposable class.
D. Use constraints to require the generic type to implement the IDisposable
interface.

The right answer is D but I don't fully understand why this is the right
answer.

Assume I create a generic class called Farm. This Farm can contain Animals
like this.
public class Farm<Animals>
{
....
}

Now to my question if this generic class Farm<Animalsimplement
IDisposable
then method Dispose can be called to dispose of the generic objects.

I mean if I want to dispose all of the Animals object I can just call the
dispose method located in
the farm class but if I call the dispose method in the Animal class
the Farm object is still there.
Can anybody make a comment about my discussion.
//Tony

Oct 10 '08 #1
3 1528
You must remember the purpose of IDisposable. If for example you have a
class that uses Windows API calls to create a handle then you would
implement IDisposable to ensure that you class releases that resource when
it is finished with. The Dispose method will be executed either via the
IDisposable method or from your class finalizer, but either way it will be
executed and will release the resource. This is an IDisposable
implementation with a finalizer!

You could additionally implement IDisposable on your Farm class which just
calls Dispose() on each of the animals held within it, in this case the Farm
would not need a finalizer because you don't need to absolutely guarantee it
is called on Farm, only on Animal (which has already been covered above).
In this case the Dispose on Farm is just a convenience.

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 10 '08 #2
Hello!

So why is alternative D better then B ?

//Tony
"Peter Morris" <mr*********@SPAMgmail.comskrev i meddelandet
news:eL**************@TK2MSFTNGP04.phx.gbl...
You must remember the purpose of IDisposable. If for example you have a
class that uses Windows API calls to create a handle then you would
implement IDisposable to ensure that you class releases that resource when
it is finished with. The Dispose method will be executed either via the
IDisposable method or from your class finalizer, but either way it will be
executed and will release the resource. This is an IDisposable
implementation with a finalizer!

You could additionally implement IDisposable on your Farm class which just
calls Dispose() on each of the animals held within it, in this case the
Farm would not need a finalizer because you don't need to absolutely
guarantee it is called on Farm, only on Animal (which has already been
covered above). In this case the Dispose on Farm is just a convenience.

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 10 '08 #3
"You are creating a generic class, and you need to dispose of the generic
objects."

It's a bit of a vague question really because it doesn't say how the class
passed will be used

public class ObjectAndAHandle<T>
{
public T Value { get; set; }
IntPtr Handle { get; }
}

You might need to dispose of THIS object *if* it had an unmanaged resource
like a file handle or something, but would have no need to call Dispose on T
unless it happens to implement it.
public class SomeKindOfList<T>
{
public readonly List<TList { get; }
}

No need to dispose of the list, but you *might* want to dispose of the
objects inside, however you cannot possibly know because it is generic!
If the question was something like:
"You want to implement a generic class list which disposes of the objects it
references when it is disposed" then the answer would be both B and D.
In summary, the question is crap :-)

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
7
by: Scott M. | last post by:
In a typical class, do I need to indicate that it implements the IDisposable interface and then create a Dispose method that implements the Dispose required by the IDisposable interface or can I...
1
by: mark.milley | last post by:
Hi all - Okay, here's a question for all you MVPs out there--I'd like to define a default implementation for an interface. While I understand that when you implement a interface, empty routines...
10
by: Sebastian | last post by:
Hi, I'm confronted with a problem that seems not to be solvable. In general: How can I override an interface member of my base class and call the overridden method from my derived class? This...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.