Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
C# Reference
C# Keywords
Modifiers
 sealed
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Language Reference
sealed (C# Reference)

The sealed modifier can be applied to classes, instance methods and properties. A sealed class cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. When applied to a method or property, the sealed modifier must always be used with override (C# Reference).

Use the sealed modifier in a class declaration to prevent inheritance of the class, as in this example:

      sealed class SealedClass 
{
    public int x; 
    public int y;
}

It is an error to use a sealed class as a base class or to use the abstract modifier with a sealed class.

Structs are implicitly sealed; therefore, they cannot be inherited.

For more information about inheritance, see Inheritance (C# Programming Guide).

// cs_sealed_keyword.cs
using System;
sealed class SealedClass
{
    public int x;
    public int y;
}

class MainClass
{
    static void Main()
    {
        SealedClass sc = new SealedClass();
        sc.x = 110;
        sc.y = 150;
        Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);
    }
}
x = 110, y = 150

In the preceding example, if you attempt to inherit from the sealed class by using a statement like this:

class MyDerivedC: SealedClass {} // Error

you will get the error message:

'MyDerivedC' cannot inherit from sealed class 'SealedClass'.

For more information, see the following sections in the C# Language Specification:

  • 10.1.1.2 Sealed classes

  • 10.5.5 Sealed methods

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker