Replies: 5 comments 17 replies
-
Interesting thinking: If a member is definitely assigned in a constructor, then it can be inferred as not required for that constructor. However, this also means that changing implementation of a constructor can accidentally break it's public contract. |
Beta Was this translation helpful? Give feedback.
-
Required properties should have a straight metadata representation so that dependency injection frameworks can support them easily. |
Beta Was this translation helpful? Give feedback.
-
I agree with the assessment that it should be possible to mark a property as required, and then if a constructor sets that property opt out of requiredness. However I think given that in the 90-99% use case there will only be one constructor, adding new language syntax for this feels a bit heavy, especially as the suggested language syntax isn't the most elegant. Instead either inferring it (as @huoyaoyuan suggests), or using a Attribute I think offer a lot more flexibility than special syntax, as adding new well known attributes such as |
Beta Was this translation helpful? Give feedback.
-
I still say this whole avenue is a mistake. There already is a way of making properties required - put them in the constructor parameters. We don't need all this additional complexity added to the language for yet another way of doing things we can already do. |
Beta Was this translation helpful? Give feedback.
-
Explicit constructor parameters works just fine until you have more than a few, and things really get ugly when inheritance is involved. The proposal for required priorities isn't random, it's intended to address an area of real pain - an area where devs regularly make mistakes and introduce bugs. If you haven't already done so, @MgSam, it would be worth reading more about the history of the idea. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/master/meetings/2021/LDM-2021-01-11.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions