Skip to content

Cannot override property with private set #27712

Description

@Rabadash8820

Version Used:
Compiler version: '4.0.1-1.21568.1 (6ab66011)'.
Language version: latest (10.0).
Visual Studio version: 17.0.4

Steps to Reproduce:

  1. Declare a base class with an auto-implemented property:
public class Base {
    public virtual int Member { get; private set; }
}
  1. Attempt to override the property in a derived class:
public class Derived : Base {
    public override int Member { get; }    // CS8080
}
  1. Same behavior and error occur whether or not Base is abstract. Declaring Derived.Member with { get; private set; } instead results in "CS0115 'Derived.Member.set': no suitable method found to override". Also, if Base.Member is declared abstract then that line fails to compile with "CS0442 'Base.Member.set': abstract properties cannot have private accessors". These latter two errors make sense but the CS8080 one seems wrong.

I am reporting this as a bug, rather than a language feature request, b/c the resulting CS8080 error seems to be almost completely undocumented. The VS link goes to the "Sorry, we don't have specifics on this C# error" page of the MS docs, a quick google turned up basically zero meaningful results, and the only mention of it in this entire repo is this unit test and this open issue from four years ago: dotnet/roslyn#23326. The code I provided in the repro steps seems like it should be valid C# to me, but if its not, then at least the documentation should be fixed.

Expected Behavior:

I am able to override auto-implemented properties declared with private set using an auto-implemented property without set.

Actual Behavior:

Overring an auto-implemented property declared with private set using an auto-implemented property without set results in undocumented CS8080 compiler error.

Metadata

Metadata

Assignees

Labels

🗺️ reQUESTTriggers an issue to be imported into Quest.adegeo/tempTemporary label for adegeofundamentals/subsvcin-prThis issue will be closed (fixed) by an active pull request.

Type

No type

Projects

Status
👀 In review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions