Required classes with optional properties #8175
Replies: 3 comments 3 replies
-
I'm all for explicit specification and not introducing new keywords unless we have to. With that in mind, if such a feature was implemented, can we treat marking a property as nullable as the explicit "optional" instead of a new keyword? What's the impact of going that route in contrast with introducing a new keyword? I'm not sure I'm for or against this right now. |
Beta Was this translation helpful? Give feedback.
-
It might be possible to use a discard public required class MyNewRequiredClass {
public string SomeRequiredString {get; set;} // this one would be required
public string? SomeOptionalString {get;set;} = _ // this one is optional on init
public string SomeDefaultValuedString {get;set;} = "Jacob is not my name"; // this one is optional as it has a default value
} |
Beta Was this translation helpful? Give feedback.
-
I'm wondering how prevalent is that "90+% of properties [being] marked as required", but if I look at it purely from a language perspective, I'd be cautious about magic keywords that flip the behaviour of language constructs. If I look at a property now I'm able to tell how it behaves. By introducing class-wide |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey! So on C# discord we were discussing about the possibility of having a
required
orstrong
class where all member properties are automatically required and optional properties need to be marked withoptional
.Usage would look like so:
Alternatively as pointer out by iam3yal we could have something like this without introducing new keywords:
It would greatly help us in our workflow as 90+% of properties we use are marked as
required
anyways. Would love to see it in the language.Beta Was this translation helpful? Give feedback.
All reactions