Should CS9032 validate constructor visibility instead of type visibility for sealed classes? #7618
Unanswered
bbascarevic
asked this question in
General
Replies: 2 comments 2 replies
-
See https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-01-05.md#required-properties-accessibilities, where discussed and disallowed the scenario for now. We could conceivably relax it, but there are some very thorny issues around inheritance to work out. For example, what if a derived type added a public constructor? What does that do to the base required internal property? |
Beta Was this translation helpful? Give feedback.
2 replies
-
This is especially useful for async factories with private ctor. |
Beta Was this translation helpful? Give feedback.
0 replies
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.
Uh oh!
There was an error while loading. Please reload this page.
-
I believe that the visibility validation for property setters/initializers (CS9032) could be overly-restrictive in its current form. Consider the following code:
The above code yields CS9032 errors during compilation because both setters are less visible than the enclosing type. I assume the reasoning is to prevent the definition of uninitializable (and thereby effectively uninstantiable) classes.
Whether or not a class is instantiable is determined by its constructors and their visibility at the call site. For non-sealed classes, a derived class can have more visible constructors than the base class' constructors. This can lead to a situation where a class is uninitializable, because a required property writer is not accessible at the call site while the constructor is.
However, for sealed classes there's no way to "upgrade" the visibility of constructors. So for sealed classes, should CS9032 verify that property writers of
required
properties are "at least as accessible as the most accessible constructor", instead of "at least as accessible as the enclosing class"?Beta Was this translation helpful? Give feedback.
All reactions