473,805 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1834
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.co m

"Cody Powell" <co**@codypowel l.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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**@codypowel l.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.co m>
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.Dis pose(). 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.Dis pose(). 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.Di spose 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.Dis pose( ) {...}

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.Clos e( ) 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
1550
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 name="length-unit" value="cm"/> so if name="person-id", then value can contain only one of value "secure-card","thumb-print".
3
6077
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 Dispose method of IDbConnection. This Dispose method is called from a destructor. In the dispose method, i also call GC.SuppressFinalize(this) so as to avoid finalizer. All this was OK untill i came across one article that says - not to call...
4
1935
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 release refrence from memory ? E. If it release object from memory then what GC does ? 2. Which class can have dispose method ? class which is member of IDispose Inteface 3. Where we should use abstract / Interface ?
1
1795
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 heard every 15 minutes was a good starting point. Thx, Flick
7
2129
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 drives. Small relations are on one EIDE drive, and the logfiles are on the other EIDE drive. When running the task, below, the rest of the machine is not doing much. I do not remember where I saw it, but somewhere I got the idea that the number...
1
4123
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 assigned to it. I would like to set the attributes protected AND override but I can only set one, if I set the other it cancels out the previous. Does anyone know how to do this? CodeMemberMethod newMethod = new CodeMemberMethod(); ...
156
5915
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 inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I dispose of it unless I do the following:
9
1640
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 them use them but dont define them (for future revisions of the class as
0
1349
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 wrapper around T* with some extra smarts including reference counting to make memory management easier. Below, Reference<Objectwill be denoted simply by Ref. Since you only get your hands on Reference<Tand never on a T (or T*) directly, my thumb rule...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.