13

I've the following class:

namespace Warnings
{
    public abstract class BaseWarningIntField : IWarningInnerDataField
    {
        public string PropName;

        public string HeaderCaption;

        public sealed WarningInnerDataType DataType
        {
            get { return WarningInnerDataType.Integer; }
        }
    }
}

I want the last property DataType to be not overridable, since that's the base class for a warning-detail field of type Integer, so it needs to always return the correct type WarningInnerDataType.Integer.

Anyway, the compiler give me the following error:

'Warnings.BaseWarningIntField.DataType' cannot be sealed because it is not an override

But, as far as I know the override does exactly the opposite of what I'm trying to achieve.

Teejay
  • 7,210
  • 10
  • 45
  • 76

1 Answers1

22

in C# all methods by default are non-virtual. You can't override non-virtual method in sub-classes. So leaving property as usual will safe you from subclass overriding it.

Sealed is a keyword used in class declaration for inheritance restrictions or is used to stop virtual chain of members of a class hierarchy. But again - this relates to virtual methods and properties.

Trying to override "normal" property in sub-class will result in compile error

'WarningIntField.DataType.get': cannot override inherited member 'BaseWarningIntField.DataType.get' because it is not marked virtual, abstract, or override

To answer you comment, I'll present some code examples to illustrate my point. You can't actually restrict derived classes from hiding a method or property. So next situation is legal and there is no way to overcome it (this related to virtual method and methods denoted with new keyword as well)

class BaseClass
{
    public string Property {get; set;}
}

class DerivedClass : BaseClass
{
    //compiler will give you a hint here, that you are hiding a base class prop
    public string Property {get; set;}
}

The same way you can't restrict of hiding a field in a class by local variable, so this situation is also valid. Note that compiler will also help you to note, that you are hiding class field in by a local variable. This also related to readonly const and simple static fields as well.

int field = 0; //class field
void Foo()
{
    int field = 0; //local variable
}
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • 4
    From [MSDN](http://msdn.microsoft.com/en-us/library/88c54tsw(v=vs.110).aspx): _You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties._ – Austin Salonen Mar 27 '13 at 15:55
  • @AustinSalonen thanks for noting. Forgot, that you can stop virtual chain by sealing. But again, this is used for virtual methods – Ilya Ivanov Mar 27 '13 at 15:56
  • Ok, I got it. But is there a way to block others from hiding my method? Presently anyone can do that writing one with the same name. – Teejay Mar 28 '13 at 08:01
  • @Teejay I've added some explanations and codes to the answer you'r comment – Ilya Ivanov Mar 28 '13 at 09:43
  • 1
    Interesting. So `sealed` specifically means "prevent this already virtual method from being overridden further", rather than simply "prevent this method from being overridden". – Stewart Nov 21 '17 at 18:12