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.

An overridden property in C# allows a derived class to replace or extend the implementation of a property inherited from a base class. To enable this mechanism, the base class property must be explicitly marked with the virtual, abstract, or override modifier, and the derived class must declare the property using the override keyword with an identical signature.
public abstract class BaseEntity
{
    // Virtual property: provides a default implementation
    public virtual int StatusLevel { get; set; }

    // Abstract property: defines the signature, forces implementation in derived class
    public abstract string Identifier { get; }
}

public class SpecificEntity : BaseEntity
{
    // Overriding a virtual property
    public override int StatusLevel
    {
        get => base.StatusLevel; 
        set => base.StatusLevel = value; 
    }

    // Overriding an abstract property (read-only)
    public override string Identifier => "Entity-123";
}

Compiler Rules and Constraints

To successfully override a property, the C# compiler enforces strict structural rules:
  • Signature Matching: The overriding property must have the exact same name and data type as the inherited base property.
  • Modifier Prerequisites: You cannot override a standard (non-virtual) property. The base property must be virtual, abstract, or an already overridden property (override). It cannot be static.
  • Accessibility Parity: The access modifier of the overriding property must exactly match the base property (e.g., a protected virtual property must be overridden as protected override).
  • Accessor Limitations: A derived class cannot add new accessors. If the base property is read-only (only has a get accessor), the overriding property cannot introduce a set accessor.
  • Asymmetric Accessor Modifiers: If the base property defines different access levels for its accessors (e.g., a public property with a protected set), the overriding accessors implicitly inherit those exact accessibility levels. You must omit the access modifier in the derived class (e.g., writing set { ... }). Explicitly applying an access modifier to an overridden accessor results in compiler error CS0275 (accessibility modifiers may not be used on accessors in an override).
public class BaseClass
{
    public virtual int Value { get; protected set; }
}

public class DerivedClass : BaseClass
{
    public override int Value 
    { 
        get => base.Value; 
        // The 'set' implicitly remains 'protected'. 
        // Writing 'protected set' here causes CS0275.
        set => base.Value = value; 
    }
}

Accessing the Base Implementation

When overriding a virtual property, the derived class can invoke the parent class’s property accessors using the base keyword. This is strictly used to extend, rather than completely replace, the base logic. Abstract properties do not have a base implementation to call.
public override int StatusLevel
{
    get 
    {
        // Invokes the parent class's get accessor
        return base.StatusLevel; 
    }
    set 
    {
        // Invokes the parent class's set accessor
        base.StatusLevel = value; 
    }
}

Sealing an Overridden Property

If a property is overridden, it remains virtual to any further derived classes. To terminate the inheritance chain and prevent deeper subclasses from overriding the property again, apply the sealed modifier alongside override.
public sealed override int StatusLevel { get; set; }

Covariant Return Types (C# 9.0+)

Starting with C# 9.0, overridden read-only properties support covariant return types. This means the overriding property can declare a return type that is more derived (more specific) than the return type declared in the base property.
public abstract class BaseFactory
{
    public abstract BaseEntity Product { get; }
}

public class SpecificFactory : BaseFactory
{
    // Valid in C# 9.0+: SpecificEntity derives from BaseEntity
    public override SpecificEntity Product => new SpecificEntity(); 
}
Master C# with Deep Grasping Methodology!Learn More