473,394 Members | 1,718 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,394 software developers and data experts.

Rule of Thumb for a Protected Dispose() Method?

Greetings all,

I'm wondering if there's a rule of thumb related to objects that
implement IDisposable, but whose Dispose() methods are actually marked
as protected. If you dig around System.IO, you'll see a lot of these,
like StreamReader, StreamWriter, etc. Is it still a best practice to
wrap those objects in a using block?

All of the MSDN docs DO wrap such objects in a using block, but I'm
wondering if that actually accomplishes anything. Also, does anyone
know why the Dispose method is marked as protected on such objects?
Thanks for any insight you can provide,

Cody

Nov 17 '05 #1
6 1811
Cody,

The pattern for implementation indicates that the protected version is
used so that it can be overriden in derived classes (so that you can
properly dispose of the base).

However, the base class implementation of IDisposable is always going to
call the virtual/overriden method (the protected version takes a parameter,
and that doesn't satisfy the interface contract).

In short, yes, always use a using block. It allows for proper cleanup
of classes which implement IDisposable.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Cody Powell" <co**@codypowell.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Greetings all,

I'm wondering if there's a rule of thumb related to objects that
implement IDisposable, but whose Dispose() methods are actually marked
as protected. If you dig around System.IO, you'll see a lot of these,
like StreamReader, StreamWriter, etc. Is it still a best practice to
wrap those objects in a using block?

All of the MSDN docs DO wrap such objects in a using block, but I'm
wondering if that actually accomplishes anything. Also, does anyone
know why the Dispose method is marked as protected on such objects?
Thanks for any insight you can provide,

Cody

Nov 17 '05 #2
Nicholas, thanks for the response.

I have one more question that's actually based on one of your comments.
If the protected version of Dispose takes a parameter and thus doesn't
satisfy the interface contract, how is something like StreamReader
actually implementing IDisposable?

In order to fulfill that contract, Dispose() has to be public and
accept zero parameters. I don't see anything that satisfies the
condition in the object or in the base class from which it's derived.
How is this working?

Thanks for clueing me in,

Cody

Nov 17 '05 #3
Cody Powell <co**@codypowell.com> wrote:
I'm wondering if there's a rule of thumb related to objects that
implement IDisposable, but whose Dispose() methods are actually marked
as protected. If you dig around System.IO, you'll see a lot of these,
like StreamReader, StreamWriter, etc. Is it still a best practice to
wrap those objects in a using block?

All of the MSDN docs DO wrap such objects in a using block, but I'm
wondering if that actually accomplishes anything. Also, does anyone
know why the Dispose method is marked as protected on such objects?
Thanks for any insight you can provide,


As Nick said, this is a general pattern which certainly *doesn't* mean
you shouldn't call the normal Dispose method.

See
http://tinyurl.com/2t03
http://tinyurl.com/bdw6g
for more details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
StreamReader doesn't implement IDisposable. but if you take a look at the
base class TextReader which StreamReader derives from, you will see that it
has a explicit implementation of IDisposable.Dispose(). So StreamReader gets
the interface through inheritance.

and this is where the protected version of Dispose(bool) becomes important,
it allows StreamReader properly clean up after itself (by overriding),
without having to implement the interface seperately.

"Cody Powell" wrote:
Nicholas, thanks for the response.

I have one more question that's actually based on one of your comments.
If the protected version of Dispose takes a parameter and thus doesn't
satisfy the interface contract, how is something like StreamReader
actually implementing IDisposable?

In order to fulfill that contract, Dispose() has to be public and
accept zero parameters. I don't see anything that satisfies the
condition in the object or in the base class from which it's derived.
How is this working?

Thanks for clueing me in,

Cody

Nov 17 '05 #5
But TextReader <i>doesn't</i> actually have the proper Dispose()
method. Check it out in Intellisense and in the documentation:

http://msdn.microsoft.com/library/de...thodsTopic.asp

Daniel Jin wrote:
StreamReader doesn't implement IDisposable. but if you take a look at the
base class TextReader which StreamReader derives from, you will see that it
has a explicit implementation of IDisposable.Dispose(). So StreamReader gets
the interface through inheritance.

and this is where the protected version of Dispose(bool) becomes important,
it allows StreamReader properly clean up after itself (by overriding),
without having to implement the interface seperately.

"Cody Powell" wrote:
Nicholas, thanks for the response.

I have one more question that's actually based on one of your comments.
If the protected version of Dispose takes a parameter and thus doesn't
satisfy the interface contract, how is something like StreamReader
actually implementing IDisposable?

In order to fulfill that contract, Dispose() has to be public and
accept zero parameters. I don't see anything that satisfies the
condition in the object or in the base class from which it's derived.
How is this working?

Thanks for clueing me in,

Cody


Nov 17 '05 #6
I partly answered my own question, but it raised new questions.

http://msdn.microsoft.com/library/de...thodsTopic.asp.
The main frame of the page shows that the only Dispose method is the
protected Dispose(bool), which is not satisfying IDisposable. But look
in the small frame at the left. Only there, you see
"IDisposable.Dispose Method". Click on it, and you'll see that
this is the public no-parameter version that we were looking for. It
says "This member supports the .NET Framework infrastructure and is
not intended to be used directly from your code."

This method is declared inside the TextReader class like so: private
void IDisposable.Dispose( ) {...}

This is an "explicit interface implementation", meaning that you
can only see it when you've casted your object to IDisposable. But,
it's private!

MSDN doc on explicit interface implementation says "A class
implementing a file abstraction, for example, would likely implement a
Close member function that has the effect of releasing the file
resource, and implement the Dispose method of the IDisposable interface
using explicit interface member implementation".

Which raises new questions:

1) Why on earth would you want to hide this method? It breaks
Intellisense, so you can't tell that you should use a using
statement. If you want to hide it, then declaring it as IDisposable
doesn't *DO* anything.

2) How on earth can a private method satisfy an interface?

3) How on earth can the using statement call a private method?

4) What in the flying heck does TextReader.Close( ) do? Does it call
the private Dispose( )? Can we assume that it does, completely going
against every recommendation I've ever read about IDisposable,
Close(), and using?

Nov 17 '05 #7

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

Similar topics

2
by: ocelka | last post by:
I need know how define xml schema for this xml fragment: <set name="person-id" value="secure-card"/> <set name="person-id" value="thumb-print"/> <set name="length-unit" value="inch"/> <set...
3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
1
by: John | last post by:
I can't find this in the manuals. I'm not interested in size of primary or secondary logs, what is a good rule of thumb for how many logs to cut in an hour in a busy OLTP and DW environment. I...
7
by: Jean-David Beyer | last post by:
I have six hard drives (4 SCSI and 2 EIDE) on my main machine with parts of a database on each drive. The main index is on one SCSI drive all to itself. The main data are on the other three SCSI...
1
by: Jax | last post by:
Here is the CodeDom code for the standard Dispose method of a windows form application. The problem I have is with the Attributes property, it isn't a collection so only one attribute can be...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
9
by: utab | last post by:
Dear all, How do experienced programmers using this group use the rule of thumb,namely copy ctor, assignment operator, and dtor. example if the class does not need them dont use and define...
0
by: Gonçalo Rodrigues | last post by:
Hi all, I have a single-rooted hierarchy of heap-allocated objects -- call the root Object. These objects are handled via a smart pointer template Reference<T>. Basically, Reference<Tis a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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...

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.