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 abstract property in C# is a property declaration that provides no implementation and acts as a strict architectural contract. It mandates that any non-abstract derived type must provide the concrete implementation for the property’s defined accessors (get, set, or init). When inheriting from an abstract class, this implementation is provided using the override modifier.

Syntax and Implementation

An abstract property is defined using the abstract keyword. Its accessors end with a semicolon rather than a code block.
public abstract class BaseClass
{
    // Abstract property with both get and set accessors
    public abstract string Identifier { get; set; }

    // Abstract read-only property
    public abstract int Threshold { get; }

    // Abstract property with restricted accessor visibility
    public abstract double Factor { get; protected set; }
}

public interface IConfigurable
{
    // Explicitly abstract property in an interface (C# 8.0+)
    abstract string ConfigKey { get; }
}

public class DerivedClass : BaseClass, IConfigurable
{
    private string _identifier;

    // Overriding a read-write property (can use backing fields or auto-properties)
    public override string Identifier 
    { 
        get => _identifier; 
        set => _identifier = value; 
    }

    // Overriding a read-only property
    public override int Threshold => 100;

    // Overriding a property with restricted accessor visibility
    public override double Factor { get; protected set; }

    // Implementing the interface abstract property
    public string ConfigKey => "DefaultKey";
}

Core Mechanics and Compiler Rules

  • Declaration Context: Abstract properties can be declared inside classes explicitly marked with the abstract modifier. Since C# 8.0 (Default Interface Methods), properties inside interfaces can also be explicitly marked with the abstract keyword.
  • No Body: The base declaration cannot contain any logic. Writing get { return 0; } on an abstract property will result in a compiler error.
  • Mandatory Implementation: Any concrete (non-abstract) class inheriting from the base type is required by the compiler to implement the abstract property. For abstract base classes, this requires the override keyword.
  • Signature Matching: The overriding property in the derived class must exactly match the type, name, and access modifiers of the abstract property.
  • Accessor Parity: The derived class must implement exactly the accessors defined in the base abstract property.
    • If the abstract property defines only a get accessor, the derived class must provide only a get accessor. Attempting to add a set accessor to the overridden property results in compiler error CS0546 (cannot override because it does not have an overridable set accessor).
    • If the abstract property defines both get and set, the derived class must implement both.
  • Accessor Accessibility: If an abstract property restricts the visibility of a specific accessor (e.g., public abstract int Value { get; protected set; }), the overriding property must enforce the exact same visibility modifier on that specific accessor.
Master C# with Deep Grasping Methodology!Learn More