❔ `required` keyword
what is the purpose of the
required
keyword when we can enforce a property to be set through the constructor?17 Replies
from the docs:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
this is particularly useful when you have an object with many optional properties and you don't want to make a million constructor overloads
required members require to be settable
is there a way to avoid that?
You could use the
init
keywordit seems like a bug that this is considered "setting", this should act as it was still the ctor assigning a value to
Sex
needless to say, init did work
well, i guess
what im confused about is what's the difference between what the constructor does, and an "object initializer"
it lets you do this without a constructor
would this mean, that i can enforce setting MyProperty and sparing me the trouble of overloading the constructor?
yes
the problem with constructors is that they are pretty verbose
for every member that you require by using a constructor, you have to add a parameter and add a line in the constructor
then, for every derived class, you have to repeat all of those parameters, pass them to the base class's constructor, and then add more parameters and assignments in the derived class's constructor
i understand now. but does this make ctors redundant and not worht using anymore? what's a usecase where you'd want a ctor
well if you have any logic or validation in the constructor, you cannot use that on required members
the object initializer strictly happens after the constructor. that is why there is
init
and all this stuffcant that be delegated to the setter methods
it can if you only validate one parameter at a time
if you need to compare multiple of the parameters to ensure the object is valid, it does not really work
like. if you have a "minimum" and "maximum," you might need to make sure the maximum is bigger than the minimum. this is kind of hard to do with required members
also, constructors are certainly shorter to use than required members sometimes. so there is a style aspect as well
i understand
like
is equivalent to
except it doesnt allow you to use the object until
MyProperty
is set
so it must be settable, else initiable
but if the benefit of required
is to spare us having to define constructor overloads, what about "optional" parameters for the constructor? is that not a thing?what do you mean by "what about optional parameters"
you can use optional parameters in constructors, just like you can use them in methods
oh nevermind i understand now
The only place I have found this keyword usable is when defining response DTO. I usualy do not create constructors for them and use object inializing approach. Required keyword helps to remember that some fields should be always set, also when using nullable reference types it silence warnings "value is not set" since it always will be set and also improves OpenAPI spec
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.