472,805 Members | 4,066 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 9617
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.