C
C#8mo ago

❔ `required` keyword

what is the purpose of the required keyword when we can enforce a property to be set through the constructor?
17 Replies
Jimmacle
Jimmacle8mo ago
from the docs:
The required modifier indicates that the field or property it's applied to must be initialized by an object initializer.
The required modifier indicates that the field or property it's applied to must be initialized by an object initializer.
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
fæ
8mo ago
required members require to be settable is there a way to avoid that?
arion
arion8mo ago
You could use the init keyword
fæ
8mo ago
it seems like a bug that this is considered "setting", this should act as it was still the ctor assigning a value to Sex
No description
fæ
8mo ago
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"
reflectronic
reflectronic8mo ago
it lets you do this without a constructor
fæ
8mo ago
//let's assume MyProperty is required
MyObject myObjectInstance = new MyObject(param1, param2)
{
MyProperty = someUsefulValue
};
//let's assume MyProperty is required
MyObject myObjectInstance = new MyObject(param1, param2)
{
MyProperty = someUsefulValue
};
would this mean, that i can enforce setting MyProperty and sparing me the trouble of overloading the constructor?
reflectronic
reflectronic8mo ago
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
fæ
8mo ago
i understand now. but does this make ctors redundant and not worht using anymore? what's a usecase where you'd want a ctor
reflectronic
reflectronic8mo ago
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 stuff
fæ
8mo ago
cant that be delegated to the setter methods
reflectronic
reflectronic8mo ago
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
fæ
8mo ago
i understand like
MyObject myObjectInstance = new MyObject(param1, param2)
{
MyProperty = someUsefulValue
};
MyObject myObjectInstance = new MyObject(param1, param2)
{
MyProperty = someUsefulValue
};
is equivalent to
MyObject myObjectInstance = new MyObject(param1, param2);
myObjectInstance.MyProperty = someUsefulValue;
MyObject myObjectInstance = new MyObject(param1, param2);
myObjectInstance.MyProperty = someUsefulValue;
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?
reflectronic
reflectronic8mo ago
what do you mean by "what about optional parameters" you can use optional parameters in constructors, just like you can use them in methods
fæ
8mo ago
oh nevermind i understand now
Legende
Legende8mo ago
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
Accord
Accord8mo ago
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.
Want results from more Discord servers?
Add your server
More Posts