473,406 Members | 2,816 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,406 software developers and data experts.

C# Classes with members that implement IDisposable

For any class I write in C# that has a member variable that implements
IDisposable my class implements the IDisposable pattern. I do this to
guarantee the reference to the member is explicitly released and the
object is eligible for garbage collection when my class is disposed or
its' finalizer is called by GC.

My question is whether using this approach is even necessary? My
feeling is yes because it ensures the member is explicitly Disposed
when my Class is, but I'm not certain it provides any real benefit
since the GC will release the reference when it eventually gets to it.

Thanks for the input.

Nov 8 '07 #1
4 9667
You are right, in a class that follows the guidelines put forth by MS
for implementing IDisposable, if you do not call Dispose, the GC will
ultimately dispose of the classes.

However, the class implements IDisposable as an indicator to let you
know that you should take the lifetime of the instance into account, and
call Dispose on it when you are done with it, usually because the resource
is valuable. Things like socket connections, database connections, file
handles and the like all are great candidates for object wrappers with
IDisposable implementations.

So if you have a member field which is an IDisposable instance, and it
lives for the life of your object (as opposed to a specific lifetime defined
by accessing methods/properties on your class), then you should implement
IDisposable, and when using the class, call Dispose when done with the
instance.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<cg*********@hotmail.comwrote in message
news:11**********************@e34g2000pro.googlegr oups.com...
For any class I write in C# that has a member variable that implements
IDisposable my class implements the IDisposable pattern. I do this to
guarantee the reference to the member is explicitly released and the
object is eligible for garbage collection when my class is disposed or
its' finalizer is called by GC.

My question is whether using this approach is even necessary? My
feeling is yes because it ensures the member is explicitly Disposed
when my Class is, but I'm not certain it provides any real benefit
since the GC will release the reference when it eventually gets to it.

Thanks for the input.

Nov 8 '07 #2
On Nov 8, 9:52 am, cgarcia0...@hotmail.com wrote:
For any class I write in C# that has a member variable that implements
IDisposable my class implements the IDisposable pattern. I do this to
guarantee the reference to the member is explicitly released and the
object is eligible for garbage collection when my class is disposed or
its' finalizer is called by GC.

My question is whether using this approach is even necessary? My
feeling is yes because it ensures the member is explicitly Disposed
when my Class is, but I'm not certain it provides any real benefit
since the GC will release the reference when it eventually gets to it.

Thanks for the input.
Hi... The real benefit of implementing IDisposable it enables the
application to clean up un-managed resources. Examples include
database connections, file handles and graphics handles (bitmaps,
drawing surfaces, etc.). The .NET classes that manage these resources
are light wrappers around the Windows API. Via the Windows API, these
resources need to be explicitly released.

Are you saying that if your class contains a member that implements
IDisposable, you implement IDisposable on the class itself so that you
can call Dispose explicitly?

jpuopolo

Nov 8 '07 #3
For any class that has disposable members, you SHOULD implement the
IDisposable interface on that class and make its Dispose method call
Dispose on those disposable members, so that calling code has a way to
deterministically clean up the whole mess via explicit Dispose calls.

However, for any class that has disposable members but DOES NOT add
any raw unmanaged resources of its own, you SHOULD NOT implement the
full Dispose pattern with a finalizer. That pattern provides no
benefit whatsoever since the disposable members (or their nested
classes which hold the actual unmanaged resources) already provide
their own finalizers. You'll only slow down gargabe collection
because finalizers prevent memory from being released quickly.
--
http://www.kynosarges.de
Nov 9 '07 #4
On Nov 9, 2:11 am, Chris Nahr <dioge...@kynosarges.dewrote:
For any class that has disposable members, you SHOULD implement the
IDisposable interface on that class and make its Dispose method call
Dispose on those disposable members, so that calling code has a way to
deterministically clean up the whole mess via explicit Dispose calls.

However, for any class that has disposable members but DOES NOT add
any raw unmanaged resources of its own, you SHOULD NOT implement the
full Dispose pattern with a finalizer. That pattern provides no
benefit whatsoever since the disposable members (or their nested
classes which hold the actual unmanaged resources) already provide
their own finalizers. You'll only slow down gargabe collection
because finalizers prevent memory from being released quickly.
--http://www.kynosarges.de
Thanks everyone for your input!

Nov 12 '07 #5

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

Similar topics

2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
9
by: Kiran | last post by:
Hi, As a convention we always create interfaces before creating classes for class libraries. we then implement this interface in a class. Can someone point out the reasons\advantages for...
0
by: demon | last post by:
I'm having this class Public Class TestClass Implements IDisposable Dim Conn As SqlConnection Public Sub New(ByVal connectionString As String)
11
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or...
13
by: Carl Johansson | last post by:
Being quite new to C#, I may have misunderstood this. If so please bear with me! As far as I can understand, any instances of a class that implements the IDisposable interface must call the...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.