Use primary constructor
Use primary constructor
Microsoft docsDescription
This rule flags classes that can use a primary constructor instead of a separate constructor definition. You define a primary constructor by placing any constructor parameters in parentheses following the type name. A primary constructor indicates that these parameters are necessary for any instance of the type.
When you create a class or struct with multiple constructors, you gain flexibility, but you also add verbose syntax. You must cleanly separate the constructor input from the class state. With primary constructors, you can put one constructor's parameters in scope for the whole class or struct. You can use these parameters for initialization or directly as object state. However, if you add other constructors, they must call through the primary constructor.
Example
// Code with violations.
class C
{
public C(int i) { }
}
// Fixed code.
class C(int i)
{
}Configurable options
Vote for the value each option should take in the generated .editorconfig.
csharp_style_prefer_primary_constructorstrue