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 virtual property in C# is a class property declared with the virtual modifier, indicating that its get, set, or init accessors can be overridden by derived classes. It establishes a default implementation in the base class while permitting subclasses to modify or entirely replace that behavior using the override modifier, thereby enabling runtime polymorphism.

Technical Characteristics

  • Default Implementation: Unlike abstract properties, a virtual property must provide a complete implementation in the base class. This can be an auto-implemented property or a property with explicit backing fields.
  • Optional Overriding: Derived classes are not forced to override a virtual property. If omitted, the derived class inherits the base class’s accessors.
  • Signature Parity and Covariance: Generally, the overriding property must exactly match the type, name, and access modifiers of the base virtual property. However, since C# 9, C# supports covariant return types for read-only (get-only) properties. This allows an overriding get-only property to return a more derived type than the base virtual property.
  • Accessor Constraints: A derived class can only override the accessors defined in the base class. If the virtual property is read-only, the overriding property cannot add a set or init accessor.
  • Init Accessor Enforcement: If a base virtual property utilizes an init accessor (introduced in C# 9) instead of a set accessor, the overriding property must strictly respect this semantic. The derived class must also use init and cannot change the inherited init accessor into a set accessor.
  • Base Invocation: Overriding accessors can invoke the parent class’s implementation using the base keyword, allowing developers to extend rather than completely replace the original logic.
  • Sealing: An overridden virtual property can be marked with the sealed modifier in a derived class to prevent further overriding in subsequent derived classes.

Syntax Visualization

public class BaseClass
{
    // Auto-implemented virtual property
    public virtual string Identifier { get; set; } = "Default-ID";

    // Virtual property with an init accessor
    public virtual int Configuration { get; init; }

    // Read-only virtual property returning a base type
    public virtual object ReadOnlyData { get; } = new object();
}

public class DerivedClass : BaseClass
{
    // Overriding the standard get/set property
    public override string Identifier
    {
        get => base.Identifier;
        set => base.Identifier = value.Trim();
    }

    // Overriding must maintain the 'init' accessor; 'set' is not allowed here
    public override int Configuration
    {
        get => base.Configuration;
        init => base.Configuration = value;
    }

    // Covariant return type (C# 9+): returning a more derived type (string instead of object)
    public override string ReadOnlyData { get; } = "Derived String";
}

Access Modifier Asymmetry

If a virtual property defines different access modifiers for its accessors (e.g., a public get and a protected set), the overriding property must maintain that exact asymmetry explicitly.
public class BaseNode
{
    public virtual int NodeCount { get; protected set; }
}

public class ChildNode : BaseNode
{
    // The override must explicitly declare the 'protected' modifier on the setter.
    // Omitting the 'protected' keyword on the set accessor results in compiler error CS0507.
    public override int NodeCount 
    { 
        get => base.NodeCount; 
        protected set => base.NodeCount = value; 
    }
}
Master C# with Deep Grasping Methodology!Learn More