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

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

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.

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) }.

Note: In general, it is a good idea to use the new keyword when knowingly shadowing a method. It isn't required (but is a warning) because requiring it would cause applications to break when they inadvertently hid a method (i.e. , because a new version of the base class introduced a virtual method with the same name).

– Brian Jul 5 at 17:22 @Brian, thanks, good point. I've never intentionally shadowed a method, but I have often seen the compiler warning when I forget to add override. – harpo Jul 5 at 18:16.

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.

This is precisely what sealed keyword is for. msdn.microsoft.com/en-us/library/ms17315... 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.

You don't need to seal a member unless it was declared as virtual in the first place. – Jon Skeet Jul 5 at 16:28 @John - yes, I was still adding a bottom paragraph. – Jakub Konecki Jul 5 at 16:30 This doesn't prevent overriding, it merely says "in my derived class (class A), I have overridden this member, but in a derived class from this derived class (class B : A), I no longer want this member to be virtual.

" To prevent overriding in the first place, just omit the virtual keyword. – Kyle Trauberman Jul 5 at 16:30.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions