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

  • Thread starter Thread starter Néstor Marcel Sánchez Ahumada
  • Start date Start date
N

Néstor Marcel Sánchez Ahumada

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
 
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
 
Néstor Marcel Sánchez Ahumada said:
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.
 
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)
 
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
 
Back
Top