Required members: Please remove CS9032 (or at least make it a warning). #6435
Replies: 4 comments 10 replies
-
You can write an analyzer here that you can use to apply your own domain requirements that all your constructors assign this. |
Beta Was this translation helpful? Give feedback.
-
Do you mean “CS9032”? |
Beta Was this translation helpful? Give feedback.
-
I see this is a common misuse (or unintended expectation) of this required property feature. You are expecting the compiler to enforce assignment in the constructor, but the compiler does not really enforce that except simple cases, because it is too expensive to analyze all code paths. The |
Beta Was this translation helpful? Give feedback.
-
is a pattern like this suitable for your use case? public class MyClass
{
public required Class1 Dependency1 { private get; init; }
public required Class2 Dependency2 { private get; init; }
public required Class3 Dependency3 { private get; init; }
} |
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.
-
@333fred -
I just started playing with
required
members and I'm having a less-than-optimal experience due to CS9032.I believe that CS9032 should be removed completely.
Remove CS9032 because it is not DI-Friendly
Consider this use-case:
I use AutoFac for dependency injection.
I have lots of classes that look like this:
By leveraging
required
and new AutoFac settings, I want to refactor my code to look like this:However, today, I can't do that because CS9032 prevents it.
Remove CS9032 because it prevents expressing intent
Consider this code:
CS9032 prevents me from specifying an internal requirement that any constructors I create should explicitly set
Age
.Beta Was this translation helpful? Give feedback.
All reactions