C#C
C#3y ago
blokyk

✅ Override (abstract) getter-only property with a setter

Hello! I was wondering if there is anyway to add a setter to an overridden getter-only property?

Currently, I've tried the following:
public class Base {
    public abstract int Count { get; }
}

public class Derived : Base {
    // #1
    public override int Count { get; set; } // CS0546: Cannot override because no 'set' accessor

    // #2
    // CS0106: The 'override' modifier is not valid for this item
    // CS0538: 'Base' in explicit implementation is not an interface
    override Base.Count => Count;
    public new int Count { get; set; }
}


I'd prefer avoiding a constructor, since the rest of my hierarchy uses required properties, and Derived is actually sub-classed elsewhere, so adding a constructor would make things a bit clunky...
Was this page helpful?