Skip to content

Commit b515ea7

Browse files
authored
Add reason for using primary constructor
Explain the benefits and trade-offs of using primary constructors in C#.
1 parent 37c026f commit b515ea7

File tree

1 file changed

+2
-0
lines changed
  • docs/fundamentals/code-analysis/style-rules

1 file changed

+2
-0
lines changed

docs/fundamentals/code-analysis/style-rules/ide0290.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dev_langs:
2424

2525
This rule flags classes that can use a [primary constructor](../../../csharp/programming-guide/classes-and-structs/instance-constructors.md#primary-constructors) 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.
2626

27+
The ability of a class or struct in C# to have more than one constructor provides for generality, but at the expense of some tedium in the declaration syntax, because the constructor input and the class state need to be cleanly separated. Primary constructors put the parameters of one constructor in scope for the whole class or struct to be used for initialization or directly as object state. The trade-off is that any other constructors must call through the primary constructor.
28+
2729
## Options
2830

2931
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).

0 commit comments

Comments
 (0)