-
I just installed .NET 7 preview 5 and am playign with the required keyword. In one instance I have a parameterless constructor that set a required property to some value. However I get a CS9035 error (Required member must be set in the obejct initializer or attribute constructor). I wasn't expecting that. I thought setting it in the constructor would make the required requriement fulfilled. Is this the intended behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I believe you need to apply the |
Beta Was this translation helpful? Give feedback.
-
Yes, that is correct. Required members are a part of the public contract of your constructor, and we do not do any inference of public contracts in C#. |
Beta Was this translation helpful? Give feedback.
I believe you need to apply the
SetsRequiredMembers
attribute to the constructor: https://github.com/dotnet/csharplang/blob/main/proposals/required-members.md#setsrequiredmembersattribute