> ## 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.

# C# Virtual Property

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

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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; 
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
