473,508 Members | 4,779 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 12858
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.cl> wrote in message
news:Ox**************@tk2msftngp13.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.cl> 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.com>
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*****@yumspamyumYahoo.com> wrote in message
news:#g**************@TK2MSFTNGP11.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.cl> wrote in message
news:Ox**************@tk2msftngp13.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 misunderstanding (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: "Constructors 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.cl> wrote in message
news:es**************@TK2MSFTNGP09.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*****@yumspamyumYahoo.com> wrote in message
news:#g**************@TK2MSFTNGP11.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.cl> wrote in message
news:Ox**************@tk2msftngp13.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
1155
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...
6
1830
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...
43
2796
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...
5
2083
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...
9
5178
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
3408
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...
1
4667
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...
1
1833
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,...
0
1294
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...
0
7228
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
7128
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
7332
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,...
1
7058
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
7502
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...
1
5057
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...
0
4715
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...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
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.