[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 stringproperty 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 arequiredmodifier 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 stringproperty in a derived class with justoverride string { get; }when the property is initialized through the constructor or another method. Therequiredmodifier 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
requiredmodifier or a setter in the derived class, even though the property is initialized via a constructor. This restricts flexibility and forces developers to repeat therequiredmodifier or add a setter unnecessarily.Code Example:
Steps to Reproduce:
virtual requiredstring property and aninitaccessor.override string { get; }, and initialize it via the constructor.Development Environment:
.NET SDK 8.0
Visual Studio 2022
Additional Information:
The
requiredmodifier 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 therequiredmodifier 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