Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A sealed property in C# is a class or interface member that prevents derived types from overriding its implementation. In the context of class inheritance, it terminates the virtual dispatch chain for an overridden property. In the context of interfaces (introduced in C# 8.0), it locks a default property implementation, preventing derived interfaces or implementing types from providing their own explicit implementation.

Syntax and Declaration

The syntax differs depending on whether the property is declared within a class or an interface. Class Declaration: In a class, the sealed keyword must be paired with the override modifier.
public sealed override DataType PropertyName { get; set; }
Interface Declaration (C# 8.0+): In an interface, the sealed keyword is applied directly to a property that provides a default implementation, without the override modifier.
sealed DataType PropertyName { get => defaultValue; }

Technical Constraints and Rules

  1. Class Context (Requires Override): Within a class, a property can only be sealed if it is actively overriding a virtual, abstract, or previously overridden property from a base class. You cannot declare a new property as sealed in a class, nor can you seal a virtual property at the point of its initial declaration.
  2. Interface Context (Default Implementations): Within an interface, a property can be declared as sealed at its initial declaration, provided it includes a default implementation (a body). This prevents implementing classes or structs from explicitly redefining the property for that interface.
  3. Compile-Time Enforcement:
    • In classes, if a derived class attempts to use the override keyword on a sealed property, the compiler throws error CS0239.
    • In interfaces, if a type implementing the interface attempts to provide an explicit interface implementation for the sealed property, the compiler throws error CS8704. If the type defines a public property with the exact same signature, the compiler simply ignores it during interface mapping. Because Default Interface Members (DIMs) are never inherited into the implementing class’s public API, the class property is treated as a completely independent member. It does not hide or shadow the interface member, and the compiler does not emit a hiding warning (CS0108).
  4. Property-Level Application: The sealed modifier applies to the property as a single unit. You cannot seal an individual get or set accessor independently; both accessors share the sealed state of the property.
  5. Hiding via new: While a derived type cannot override a sealed class property, it can hide it using the new keyword (public new DataType PropertyName). This creates a completely separate property that does not participate in the original polymorphic chain.

Code Visualization

The following examples demonstrate the compiler behavior of sealed properties in both classes and interfaces: 1. Sealed Properties in Classes
public class BaseEntity
{
    public virtual string Identifier { get; set; }
}

public class IntermediateEntity : BaseEntity
{
    // Overriding and sealing the property
    public sealed override string Identifier 
    { 
        get => base.Identifier; 
        set => base.Identifier = value; 
    }
}

public class LeafEntity : IntermediateEntity
{
    // COMPILE-TIME ERROR CS0239: 
    // 'LeafEntity.Identifier': cannot override inherited member 'IntermediateEntity.Identifier' because it is sealed.
    /*
    public override string Identifier 
    { 
        get => "New ID"; 
        set { }
    }
    */

    // Valid: Hiding the sealed property instead of overriding
    public new string Identifier { get; set; }
}
2. Sealed Properties in Interfaces (C# 8.0+)
public interface IEntity
{
    // Sealing a default interface property implementation
    sealed string EntityType => "Standard";
}

public class ConcreteEntity : IEntity
{
    // COMPILE-TIME ERROR CS8704: 
    // 'ConcreteEntity' cannot implement interface member 'IEntity.EntityType' because it is sealed.
    /*
    string IEntity.EntityType => "Custom";
    */

    // Valid: This creates a completely independent property. 
    // Because default interface members are not inherited into the class's public API, 
    // this does not hide or shadow the interface member, and no CS0108 warning is emitted.
    public string EntityType => "Custom";
}
Master C# with Deep Grasping Methodology!Learn More