[Issue]: Оverride string without setter should be allowed for required properties when initialized via constructor #8543
Unanswered
dzhukovsky
asked this question in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In .NET 8, when overriding a
virtual required string
property from a base class, it’s currently not allowed to simply declareoverride string { get; }
in the derived class if the property is initialized via a constructor. The requirement for arequired
modifier in the derived class or a setter seems unnecessary in cases where the property is already initialized through the constructor.Expected Behavior:
It should be possible to override a
virtual required string
property in a derived class with justoverride string { get; }
when the property is initialized through the constructor or another method. Therequired
modifier should not need to be repeated in the derived class, as the requirement is already enforced by the base class.Actual Behavior:
The compiler enforces the presence of the
required
modifier or a setter in the derived class, even though the property is initialized via a constructor. This restricts flexibility and forces developers to repeat therequired
modifier or add a setter unnecessarily.Code Example:
Steps to Reproduce:
virtual required
string property and aninit
accessor.override string { get; }
, and initialize it via the constructor.Development Environment:
.NET SDK 8.0
Visual Studio 2022
Additional Information:
The
required
modifier should only be needed in the base class to enforce initialization. When overriding in a derived class, developers should be able to simply useoverride string { get; }
without being forced to repeat therequired
modifier or include a setter, as long as the property is properly initialized through other means (e.g., a constructor).Beta Was this translation helpful? Give feedback.
All reactions