none
What is the purpose of “sealed” in C# when “virtual” is optional RRS feed

  • Question

  • You can override a virtual method or property and  seal it so that it cannot be overridden further in the inheritance hierarchy. That is the use of sealed methods.

    But what would be the use of sealed classes.I know that sealed classes cannot be inherited but what is the point  when we have an optional virtual .When we do not mention virtual then the derived class method cannot be overriden right ?

    Monday, June 11, 2018 10:35 AM

All replies

  • I have never used a sealed class. However,  I have used sealed methods in a base class where the code in the sealed method couldn't be overridden. The code was static and it is to be used as/is without the possibility of the code in the method being overridden and changed.

    https://www.c-sharpcorner.com/article/sealed-class-in-C-Sharp/

    Monday, June 11, 2018 12:34 PM
  • "When we do not mention virtual then the derived class method cannot be overriden right ?"

    Sealed classes have nothing to do with virtual methods. You can always create a derived type from a base ref type (class in C#) unless it has no callable constructors or it is sealed.

    public class Base ()
    {
    }
    
    public class Derived : Base
    {
    }

    And, at least in C#, you can technically override a non-virtual member using the new keyword. It is rarely done though because it does not (purposefully) behave correctly in a lot of cases.

    The primary scenarios where a sealed class is useful that I've seen.

    1. You have a base/derived hierarchy and you've gone as far as you want down the inheritance tree so you seal the class. This is a performance optimization because the compiler knows there won't be any more derived types and can make some assumptions that otherwise wouldn't work. This is most commonly done when creating nested, implementation classes.
    2. A type is designed to only be used in a very specific scenario and therefore creating derived types doesn't make sense. Sealing the class will enforce that. Prior to static classes being added this was one of the steps used in setting up a singleton. It was also heavily used in the original framework but has since been dropped as more of a limitation then a benefit. String is an example of a sealed class.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Dolen Zhang Tuesday, June 12, 2018 7:59 AM
    Monday, June 11, 2018 1:51 PM
    Moderator
  • Very strictly, each concrete class should be sealed. It is because concrete class implements concrete behavior and it is bad idea to change behavior of concrete class. Yes it is very strict rule and there are some cases when inheritance level could be greater. 

    But more inheritance level is harder to read and decrease maintainability and increase complexity of system. 


    • Edited by Petr B Wednesday, July 4, 2018 8:15 AM
    Monday, June 11, 2018 2:29 PM
  • Hi,

    If we don't want our own classes to be inherited; if some classes are no longer necessary for inheritance, then we can use the sealed modifier to declare in the class, so that the class can not derive other classes, that is called sealed class.

    Refer: sealed (C# Reference)

    virtual (C# Reference)

    Abstract and Sealed Classes and Class Members (C# Programming Guide)

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 4, 2018 5:52 AM