Feature Request: Allow required Modifier on Entire record Type #9518
Unanswered
Manu-APO
asked this question in
Language Ideas
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 C# 9.0,
required
modifiers were introduced for properties in arecord
type, making it easier to ensure properties are initialized during object creation. However, developers currently need to explicitly mark each property with therequired
keyword to enforce this behavior:While this works fine, it can become cumbersome when dealing with large
record
types that have many properties. It would be useful if C# allowed therequired
modifier to be applied to the entirerecord
type, automatically marking all properties as required without the need to annotate each one individually.Suggested Change
Allow the
required
modifier to be applied at the record level, so all properties in therecord
are implicitly considered required. This would eliminate the need to annotate each individual property withrequired
.For example, the following code would automatically apply the
required
modifier to all properties:In this example, all the properties would be required, meaning they must be initialized when the
EmailSettingsModel
is created, but we avoid the repetitive application ofrequired
to each property.Benefits
record
types with many properties, this change would eliminate the need to repeatedly apply therequired
modifier, reducing boilerplate.required
modifier across arecord
, it would be consistent across all properties without needing to track each individual one.required
modifier easier to apply in scenarios where all properties should be mandatory to initialize, streamlining the definition of configuration models, data transfer objects (DTOs), etc.Example Scenario
Consider a large configuration model that has many properties that must be initialized during object creation:
In this case, marking
record
asrequired
would ensure that all properties are required to be initialized, without needing to repeatrequired
for each property individually.Beta Was this translation helpful? Give feedback.
All reactions