473,414 Members | 1,716 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,414 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 4289
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: 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
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
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,...

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.