473,756 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why C# doesn't support sealed methods for base classes?

In a method declaration the 'sealed' keyword must be used with the
'override' keyword to avoid further overriding. Thus it can't be used in
base classes. Why?
This would be a good enhancement for C# 3.0?
Any comments?

Néstor
Nov 15 '05 #1
4 12875
There is no benefit confired to a sealed method that does not exist in a
base class method that is not virtual; if allowed, it would be redundant.
In other words this:

public class Base
{
public void MyMethod() { }
}

is no different to a derived class than this:

public class Child : VirtualBase
{
public override sealed MyMethod() { }
}

A derived class can still hide inherited members with the 'new' keyword, but
not override with 'override'.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Néstor Marcel Sánchez Ahumada" <nm**@terra.c l> wrote in message
news:Ox******** ******@tk2msftn gp13.phx.gbl...
In a method declaration the 'sealed' keyword must be used with the
'override' keyword to avoid further overriding. Thus it can't be used in
base classes. Why?
This would be a good enhancement for C# 3.0?
Any comments?

Néstor

Nov 15 '05 #2
Néstor Marcel Sánchez Ahumada <nm**@terra.c l> wrote:
In a method declaration the 'sealed' keyword must be used with the
'override' keyword to avoid further overriding. Thus it can't be used in
base classes. Why?


Because unless you explicitly declare something as virtual in the first
place, it can't be overridden anyway.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
The problem is when the base class is used to implement an Interface: The
methods then are converted to Virtual (ILDAsm shows it, but I can't say
where in the documentation is). Then... there is no way to prevent method
overriding or hiding :S

Néstor
(Hope the english was good)

"Richard A. Lowe" <ch*****@yumspa myumYahoo.com> wrote in message
news:#g******** ******@TK2MSFTN GP11.phx.gbl...
There is no benefit confired to a sealed method that does not exist in a
base class method that is not virtual; if allowed, it would be redundant.
In other words this:

public class Base
{
public void MyMethod() { }
}

is no different to a derived class than this:

public class Child : VirtualBase
{
public override sealed MyMethod() { }
}

A derived class can still hide inherited members with the 'new' keyword, but not override with 'override'.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Néstor Marcel Sánchez Ahumada" <nm**@terra.c l> wrote in message
news:Ox******** ******@tk2msftn gp13.phx.gbl...
In a method declaration the 'sealed' keyword must be used with the
'override' keyword to avoid further overriding. Thus it can't be used in
base classes. Why?
This would be a good enhancement for C# 3.0?
Any comments?

Néstor


Nov 15 '05 #4
My mistake: In fact... *the compiler* prevents the overriding of a method
not "explicity" declared as virtual override or abstract. Even when a method
is virtual because of interface implementation.
Sorry for my misunderstandin g (and for my english)

The origin of this question was a "CriticalWarnig " message emitted by
"FxCop" a Microsoft tool for inspect assemblies and its conformande with
..NET guidelines.
The message was: "Constructo rs should not call virtual methods defined by
the class"
With this description: "Virtual methods defined on the class should not be
called from constructors. If a derived class has overridden the method, the
derived class version will be called (before the derived class constructor
is called)."
The inspected assembly in fact was virtual, but generated by the compiler
since its inteface implementation.
Thanks,

Néstor

"Néstor Marcel Sánchez Ahumada" <nm**@terra.c l> wrote in message
news:es******** ******@TK2MSFTN GP09.phx.gbl...
The problem is when the base class is used to implement an Interface: The
methods then are converted to Virtual (ILDAsm shows it, but I can't say
where in the documentation is). Then... there is no way to prevent method
overriding or hiding :S

Néstor
(Hope the english was good)

"Richard A. Lowe" <ch*****@yumspa myumYahoo.com> wrote in message
news:#g******** ******@TK2MSFTN GP11.phx.gbl...
There is no benefit confired to a sealed method that does not exist in a
base class method that is not virtual; if allowed, it would be redundant. In other words this:

public class Base
{
public void MyMethod() { }
}

is no different to a derived class than this:

public class Child : VirtualBase
{
public override sealed MyMethod() { }
}

A derived class can still hide inherited members with the 'new' keyword,

but
not override with 'override'.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Néstor Marcel Sánchez Ahumada" <nm**@terra.c l> wrote in message
news:Ox******** ******@tk2msftn gp13.phx.gbl...
In a method declaration the 'sealed' keyword must be used with the
'override' keyword to avoid further overriding. Thus it can't be used in base classes. Why?
This would be a good enhancement for C# 3.0?
Any comments?

Néstor



Nov 15 '05 #5

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

Similar topics

3
1165
by: corner76 | last post by:
Hi all, I have designed, implemented and tested this code, and it runs perfectly. Now we're porting it to ARM11, and its compiler doesn't support virtual function in compination with something that's inherent in our application and cannot be changed. So I have to figure out how to implement virtuality myself. I have a base class (representing a prameter) with some derived classes (one per paramter type). I have a container of the base...
6
1842
by: Ben Finney | last post by:
Howdy all, Okay, so Guido doesn't like Abstract Base Classes, and interfaces are the way of the future. But they're not here now, and I understand ABCs better. I want my modules to (sometimes) define an abstract base exception class, that all other exceptions in that module inherit from. class FooException(Exception):
43
2842
by: Zeng | last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was not supported in C#. It's almost about as bad as it doesn't support the inheritance hierarchy and method reference (calling tree) browsing that is supported in C++. I don't know how some could write a large scale object-oriented application w/o those. If you have overcome these limitations in C#, please share your thoughts and ideas. Thanks!
5
2099
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write a web application but this isn't specifically a web question. I have two classes which must inherit a different System class, in a specific case my web pages must inherit System.Web.UI.Page while my user controls must inherit...
9
5200
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
12
3441
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
1
4686
by: dusanv | last post by:
Hi, I have a class (say O1) that overrides System::Windows::Forms::UserControl and then another one that overrides O1 (call it O2). O2 used to work with the Designer as it was inheriting directly from UserControl (and was created using the wizard) but since I inserted O1 into inheritance tree (without resx, straight override) I get the error pasted at the bottom when trying to open O2 in Designer. I deleted the NCB file to no avail....
1
1847
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks, Mulugeta
0
1317
by: =?Utf-8?B?Zmplcm9uaW1v?= | last post by:
Hi all, As I mentioned in a previous thread (see 'Dbghelp, symbols and templates' in microsoft.public.windbg), we created a powerful symbol engine using dbghelp to dump the contents of the stack symbols when an exception occurs. The engine is able to dereference and process UDT symbols up to their highest base class. It also supports multiple inheritance. However, we are having trouble with virtual inheritance. Documentation is very...
0
9152
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,...
0
9930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9571
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
6410
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
4996
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...
1
3676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.