Replies: 4 comments
-
Hello,
The only rule is to have public not readonly property. For the "require" part you have enterprise Library and others frameworks who propose thinks. But it's not at compilation time. |
Beta Was this translation helpful? Give feedback.
-
@StijnVA That is precisely what constructor parameters are designed for. They enforce every guarantee you need. There's no way to enforce guarantees around setting properties at an indeterminate time after the constructor has been called. So many ways to break this... |
Beta Was this translation helpful? Give feedback.
-
Although the above comments are true, i think them are missing the point. I am just proposing to be able to write: |
Beta Was this translation helpful? Give feedback.
-
You can't enforce that guarantee with properties. Imagine that the C# 7.1 compiler enforced it. How would you prevent somebody with a C# 7.0 compiler from compiling |
Beta Was this translation helpful? Give feedback.
-
Required Property Initialization
Summary
Allow an object initializer to invoke a constructor.
Motivation
Not having this feature leads to confusing mixed object initialisation syntax. It forces the writer of the code to care about the specific initialisation code.
Detailed design
Consider a class:
One could create an object of it:
While it would be nice to write this as:
Drawbacks
Having multiple constructors, it might be impossible to differentiate between the constructor used.
Having the class:
It would be unclear whether the above object construction should translate to:
Alternatives
Having a keyword that requires an property being initialized:
resulting in a compile error when not specifying a value for X or Y when constructing an object.
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions