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

Why Dispose method can not be virtual

In MSDN example for IDisposable, it has the followin
----------------------------------------------------------------------------
// Implement IDisposable
// Do not make this method virtual
// A derived class should not be able to override this method
public void Dispose(

Dispose(true)
// This object will be cleaned up by the Dispose method
// Therefore, you should call GC.SupressFinalize t
// take this object off the finalization queue
// and prevent finalization code for this objec
// from executing a second time
GC.SuppressFinalize(this)

----------------------------------------------------------------------------

Could someone explains me why "Dispose method can not be virtual, A derived class should not be able to override this method." I just want to do this in the base clas

public void Dispose(

DoDispose()
protected virtual void DoDispose(

And the derived class could override DoDispose method to do its own clean up work

Is this acceptable? Or this is confilict with "Dispose method can not be virtual,..." I just want to know the reason.

Thank you very very much

Nov 16 '05 #1
3 4282
Bonnie <an*******@discussions.microsoft.com> wrote:
Could someone explains me why "Dispose method can not be virtual, A
derived class should not be able to override this method." I just
want to do this in the base class

public void Dispose()
{
DoDispose();
}

protected virtual void DoDispose()
{
}

And the derived class could override DoDispose method to do its own
clean up work.

Is this acceptable? Or this is confilict with "Dispose method can not
be virtual,..." I just want to know the reason.


You certainly *can* make Dispose virtual. Whether you should or not is
a different matter.

Personally, I can't see that making Dispose virtual is any worse than
making Dispose(bool) virtual - derived classes which override Dispose
(bool) will still have to call base.Dispose(bool) in order to work
properly.

The only thing achieved in the example is to ensure that
SuppressFinalize is called exactly once.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Bonnie wrote:
Is this acceptable? Or this is confilict with "Dispose method can not be virtual,..." I just want to know the reason.


If the base class has any implementation, and is allocating resources
that need to be cleaned up at Dispose() time, then having a virtual
Dispose method that's overridden by an inheriting class would stop them
from being released, unless the inheriting class specifically calls the
base's Dispose method. Better to be safe than sorry here, and let each
class implement IDisposable itself.
Nov 16 '05 #3
Bonnie <an*******@discussions.microsoft.com> wrote:
Could someone explains me why "Dispose method can not be virtual, A
derived class should not be able to override this method." I just
want to do this in the base class

public void Dispose()
{
DoDispose();
}

protected virtual void DoDispose()
{
}

And the derived class could override DoDispose method to do its own
clean up work.

Is this acceptable? Or this is confilict with "Dispose method can not
be virtual,..." I just want to know the reason.


You certainly *can* make Dispose virtual. Whether you should or not is
a different matter.

Personally, I can't see that making Dispose virtual is any worse than
making Dispose(bool) virtual - derived classes which override Dispose
(bool) will still have to call base.Dispose(bool) in order to work
properly.

The only thing achieved in the example is to ensure that
SuppressFinalize is called exactly once.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
8
by: Gawelek | last post by:
When I write this code namespace WindowsApplication1 { public class Form1 : System.Windows.Forms.Form { (...) /// <summary> /// Clean up any resources being used. /// </summary>
4
by: Sunit Joshi | last post by:
Hello All I have an abstract class C1 with this: public abstract class C1 { protected bool m_Dirty; protected override void Dispose(bool disposing) { if(m_Dirty) WriteOuput();
2
by: Bonnie | last post by:
In MSDN example for IDisposable, it has the followin ---------------------------------------------------------------------------- // Implement IDisposable // Do not make this method virtual // A...
3
by: Vagabond Software | last post by:
Scenario: The end-user selects File-Save from my applications (WinForm) menu, specifies a filename, and waits waits for my app to write the contents of a dataset to an Excel workbook. The...
3
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will...
13
by: Grafix | last post by:
All - As we all know, Dispose method is reserved in C++ 8 and the expected syntax is to use ~MyClass(). In 1.1, we used to have following structure for Dispose class MyClass : IDisposable {...
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....
3
by: AlexS | last post by:
When I implement Dispose pattern in object implementing IDisposable, current fxcop recommends: Ensure that Wrapper.Dispose():Void is declared as public and sealed. However, if I do as it asks,...
5
by: Tony | last post by:
Hello! I know that the block of code below is the same as using the using clause. I must have this kind of text because the question is about the statement ((IDisposable)reader).Dispose(); ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.