Skip to content

What are sealed classes (not inheritable) in .NET?

mursaleen fayyaz

Asked Jun 2, 2013, 7:43 AM EDT2 answers
Share
LinkedInX
Hi Guys,


As example, string is a sealed class.




Come On! Get ready for innovation.



Thanks.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Sandeep Singh Shekhawat
    0

     The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.

    Classes can be declared as sealed. This is accomplished by putting the keyword sealed before the keyword class in the class definition. For example:

    Code
    public sealed class D
    {
        // Class members here.
    }

     

    A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.

     

    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. For example:

     

    Code
    public class D : C
    Code
    {
    C#
        public sealed override void DoWork() { }
    Code
    }

     Source: MSDN

    0
  • 0
    agreed. Thanks for reply.


    Suppose, string is not a sealed class in .NET. What you will do with it?
    0