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

About IDisposable.Dispose()

I heard from someone that we must not implement IDisposable for all
classes. Can someone please tell me:

1. the reason why we must not implement IDisposable for all the classes
we write.
2. what is the correct way of implementing IDisposable.Dispose?
3. what is the preferred way, if there is one over the other, of
calling the Dispose method on an object that implements IDisposable. Is
it the way that uses the "using(object){}" construct or the way that
emphasises on explicitly calling the Dispose method on the object?

Sep 11 '06 #1
6 3554
1: a better question would be "why should we implement dispose on an
object"? The answer is generally to (either):
* release an unmanaged resource in a deterministic fashion
* release any disposable managed objects that we own in a class
A Dispose method (nor a finalizer) is not the same as a C++ style
destructor; it is not really intended as a place to explicitely release
managed resources that you own - leave that to the GC.

2: erm, by providing a Dispose() method? If the class has a finalizer
(generally *only* for unmanaged wrappers), then this is common:

class SomeClass() : IDisposable {
public void Dispose() { Dispose(true); }
~SomeClass() { Dispose(false); }
protected virtual Dispose(bool disposing) {
if(disposing) {
// only release *managed* resources if disosing
// (else GC may have eaten them already)
}
// release unmanaged resources
}
}

Note that for best use it should be safe to Dispose() an object
multiple times - e.g. in Dipose() ceck if things are null (objects) / 0
(handles); if not, clean them then wipe the field so it doesn't get
re-applied if Dispose()d again.

3: "using". It evaluates to the same thing, but you are less likely to
get it wrong. And it saves time.

Marc

Sep 11 '06 #2
Water Cooler v2 wrote:
I heard from someone that we must not implement IDisposable for all
classes. Can someone please tell me:

1. the reason why we must not implement IDisposable for all the classes
we write.
It's the same reason you don't implement IEnumerator, IAsyncResult,
etc. If it's not needed don't bother. Here are few general guidelines
that may help determine if it's needed.

* DO implement IDisposable if your class directly uses unmanaged
resources. For example, a handle obtained from the Win32 API, etc.

* DO implement IDisposable if your class indirectly uses unmanaged
resources. For example, if your class has a reference to another
object that implements IDisposable.

* DO override the Finalize method if your class directly uses unmanaged

resources.

* DON'T override the Finalize method if your class does not directly
use managed resource. In other words, don't override Finalize just
because your class has a reference to another class that implements
IDisposable.

* DON'T override the Finalize method in a class who's base class
already overrides Finalize. In other words, if the base class provides
the protected Dispose(disposing as Boolean) method then you don't need
to override Finalize.

* DON'T implement IDisposable if your class does not hold unmanaged
resources either directly or indirectly. For clarity, I don't consider
objects whose scope is limited to methods as being held by the class
itself. If a method in your class uses an IDisposable object locally
then just make sure it's Dispose method is called before the method
ends.
2. what is the correct way of implementing IDisposable.Dispose?
See the MSDN documentation.

http://msdn2.microsoft.com/en-us/library/fs2xkftw.aspx

Also, consider using the SafeHandle class.
3. what is the preferred way, if there is one over the other, of
calling the Dispose method on an object that implements IDisposable. Is
it the way that uses the "using(object){}" construct or the way that
emphasises on explicitly calling the Dispose method on the object?
Well, I can tell you that I prefer to use the 'using' keyword, but
sometimes it just makes more sense to call Dispose explicitly.

Sep 11 '06 #4
Hi,

I'd just like to add that you can get the standardized disposal logic for your classes by deriving from
System.ComponentModel.Component, which has the added benefit that your class can be added to designers at design-time, if desired,
from the VS.NET toolbox.

Another reason one would implement IDisposable on a class is to manage the lifetime of an instance. e.g. TransactionScope. This
has the added benefit of supporting the C# "using" statement:

using (TransactionScope scope = new TransactionScope())
{
// TODO: something transacted
}

There are classes in the framework that implement IDisposable explicitly and provide a different method such as "Close" instead of
"Dispose". Just semantics, but you will see it from time to time. The functionality of these classes is still, commonly, the
standardized disposal logic that the other respondents mentioned.

--
Dave Sexton

"Water Cooler v2" <wt*****@yahoo.comwrote in message news:11**********************@p79g2000cwp.googlegr oups.com...
>I heard from someone that we must not implement IDisposable for all
classes. Can someone please tell me:

1. the reason why we must not implement IDisposable for all the classes
we write.
2. what is the correct way of implementing IDisposable.Dispose?
3. what is the preferred way, if there is one over the other, of
calling the Dispose method on an object that implements IDisposable. Is
it the way that uses the "using(object){}" construct or the way that
emphasises on explicitly calling the Dispose method on the object?

Sep 12 '06 #5
Great reply and I just want to add that Water Cooler v2 should try to
avoid using finalizers (i.e. ~T()). The class that contains a
finalization code should be as small as possible, just wrapper that
incapsulate these resources.

If a class implements Finalizer, you have to implement IDisposable to
give your user a possibility to explicitly clean up the resources.

Vladimir.
_____________________________
http://cogitosoft.blogspot.com/
http://landvp.com/

Sep 14 '06 #6
Vladimir,

That's an excellent point that I didn't articulate as well as I should
have. Implementing IDisposable does not imply that you should override
Finalize. But, the converse is true.

Brian

ul*******@gmail.com wrote:
Great reply and I just want to add that Water Cooler v2 should try to
avoid using finalizers (i.e. ~T()). The class that contains a
finalization code should be as small as possible, just wrapper that
incapsulate these resources.

If a class implements Finalizer, you have to implement IDisposable to
give your user a possibility to explicitly clean up the resources.

Vladimir.
_____________________________
http://cogitosoft.blogspot.com/
http://landvp.com/
Sep 15 '06 #7

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

Similar topics

9
by: Alvin Bruney [MVP] | last post by:
with the using construct, exit from scope calls dispose on said object. But what happens in a connection pooling scenario? Is the run-time smart enough to realize that Object Pooling is being used...
9
by: Razzie | last post by:
Hey all, When should or should you not inherit from the IDisposable interface for your custom classes? Say I have a custom class SomeClass that does... some stuff. If I create a new instance...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
4
by: Joey | last post by:
Hi, I have come across codes like this if(dbConn != null) { dbConn.dispose(); } and sometimes like
9
by: Hasani \(remove nospam from address\) | last post by:
I was reading a ppt ( http://www.gotdotnet.com/team/pdc/4064/tls310.ppt ) and came aross this statement. "Users can leverage a destructor. The C++ compiler generates all the Dispose code...
9
by: CMirandaman | last post by:
Perhaps someone can help me understand this. I'm trying to understand what the IDisposable interface actually does because as far as I can tell it seems to be just garnish on the plate. If I...
6
by: Water Cooler v2 | last post by:
I heard from someone that we must not implement IDisposable for all classes. Can someone please tell me: 1. the reason why we must not implement IDisposable for all the classes we write. 2....
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
3
by: Tony Johansson | last post by:
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...
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...
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
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...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.