Ivan
Ivan

Reputation: 64227

How to forbid a class method/property to be overridden in C#?

I believe I want a some methods and properties of a class to be unoverridable and use the base's implementation in all derived classes. How to achieve this? sealed keyword doesn't seem to work and says "method can not be sealed because it is not an override".

Upvotes: 10

Views: 5165

Answers (5)

Coeffect
Coeffect

Reputation: 8866

This isn't possible. A derived class can always use the new keyword to hide (not override) its parents methods. The sealed keyword simply stops the derived class from overriding a virtual method, but it could still use new to hide the base method.

Upvotes: 2

harpo
harpo

Reputation: 43188

A method that is not already an override will not be overridable unless you mark it as virtual. So it sounds like in your case no action is needed.

class A
{
    public void B() {} // can't override
    public virtual C() {} // can override
    public virtual D() {} // can override
}

The sealed modifier only applies when a method is already an override of a member in the base class. This allows you to prevent overrides in subclasses of that class.

class A1 : A
{
    public void B() {} // shadows A.B.  Not a virtual method!
    public override C() {} // overrides A.C, subclasses can override
    public override sealed D() {} // overrides A.D, subclasses cannot override
                                  // (but can shadow)
}

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1502606

Members are sealed by default in C# - unless they're marked as virtual, they can't be overridden in derived classes anyway.

They can be shadowed in derived classes, admittedly:

public new void SomeMethod()
{
}

... but that's not the same as overriding. There's no way you can prevent this, but if a caller uses a compile-time type of the base class, they won't end up calling this accidentally anyway.

If you could give us more details of exactly what you're trying to prevent (from both the caller's POV and the code being called) we may be able to help more.

Upvotes: 16

J. Tihon
J. Tihon

Reputation: 4459

The "sealed" keyword can only be used, if you override a method that is virtual, but don't want a class deriving from your implementation to override it again. Declaring the method not virtual is all you need.

As other pointed out that there is in fact a "new" keyword, which allows hiding the method. But as long as you use a reference of your base class, your base method is always called:

class BaseClass
{
     public void Foo() { Console.WriteLine("Foo"); }
}

class Derived : BaseClass
{
     public new void Foo() { Console.WriteLine("Bar"); }
}

public static void Main()
{
     Derived derived = new Derived();
     derived.Foo(); // Prints "Bar"
     BaseClass baseClass = derived;
     baseClass.Foo(); // Prints "Foo"
}

Since providing a base-class only makes sense, as long as you use a "BaseClass"-pointer everywhere, your method cannot be hidden.

Upvotes: 1

Jakub Konecki
Jakub Konecki

Reputation: 46008

This is precisely what sealed keyword is for.

http://msdn.microsoft.com/en-us/library/ms173150.aspx

A class member, method, field, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed. This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed keyword before the override keyword in the class member declaration.

If you've overridden a method from base class than use sealed. If you've declared a method in the class and don't want it to be overridden in any derived classes than don't mark it as virtual. Only virtual members can be overridden.

Upvotes: 0

Related Questions