Skip to content

Conversation

@gewarren
Copy link
Contributor

@gewarren gewarren commented Dec 23, 2025

Explain the benefits and trade-offs of using primary constructors in C#.


Internal previews

📄 File 🔗 Preview link
docs/fundamentals/code-analysis/style-rules/ide0290.md Use primary constructor (IDE0290)

Explain the benefits and trade-offs of using primary constructors in C#.
@gewarren gewarren requested a review from BillWagner December 23, 2025 18:42
@gewarren gewarren requested a review from a team as a code owner December 23, 2025 18:42
Copilot AI review requested due to automatic review settings December 23, 2025 18:42
@gewarren gewarren added the user-feedback Issues related to "Unified User Feedback". label Dec 23, 2025
@dotnetrepoman dotnetrepoman bot added this to the December 2025 milestone Dec 23, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an explanatory paragraph about the benefits and trade-offs of using primary constructors in C# to the IDE0290 style rule documentation. The addition aims to help developers understand when and why to use primary constructors versus traditional constructor syntax.

Key Changes

  • Adds a new paragraph explaining the purpose and trade-offs of primary constructors
  • Provides context about constructor generality versus declaration syntax complexity


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.

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.
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The paragraph uses passive voice ("provides for generality") and complex phrasing that doesn't follow the conversational, active voice style required by the documentation guidelines. The text should be rewritten in active voice with simpler, more direct language. Consider rephrasing to use "you" and present tense with a conversational tone.

Copilot generated this review using guidance from repository custom instructions.

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.

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.
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Markdown writing style guidelines, complex sentences should be broken up for clarity. This sentence contains multiple clauses that make it difficult to scan and understand. Break this into shorter, clearer sentences.

Copilot generated this review using guidance from repository custom instructions.

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.

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.
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The phrase "need to be cleanly separated" is imprecise and doesn't follow the guideline to use specific, concrete language. What does "cleanly separated" mean in this context? Use more precise technical terminology.

Copilot generated this review using guidance from repository custom instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dotnet-fundamentals/svc user-feedback Issues related to "Unified User Feedback".

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants