Skip to content

Update option values for prefer collection expressions #45790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

@gewarren gewarren requested a review from CyrusNajmabadi April 11, 2025 23:25
@gewarren gewarren requested a review from a team as a code owner April 11, 2025 23:25
@dotnetrepoman dotnetrepoman bot added this to the April 2025 milestone Apr 11, 2025
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

This LGTM, but I have a clarifying question for Cyrus.

|--------------------------|-------------------------------------------|---------------------------------------|
| **Option name** | dotnet_style_prefer_collection_expression | |
| **Option values** | `true` &#124; `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `List<int> list = new List<int>() { 1, 2 };`. |
| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable<int> list = new List<int>() { 1, 2 };`. The targeted type must match the type on the right-hand side or be one of the following types: <xref:System.Collections.Generic.IEnumerable%601>, <xref:System.Collections.Generic.ICollection%601>, <xref:System.Collections.Generic.IList%601>, <xref:System.Collections.Generic.IReadOnlyCollection%601>, <xref:System.Collections.Generic.IReadOnlyList%601>. |
Copy link
Member

Choose a reason for hiding this comment

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

@CyrusNajmabadi should take a look at these two lines. Currently, the collection expressions spec guarantees that IList<T> products a List<T> as the concrete type. That could mean that case should be in "when types exactly match", but I'm not sure the analyzer implemented it that way.

Copy link
Member

Choose a reason for hiding this comment

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

That could mean that case should be in "when types exactly match", but I'm not sure the analyzer implemented it that way.

The analyzer is implemented that way. When we see an ICollection<T> or IList<T> target, and we see the original type was List<T>, we will offer the change even if hte setting is "when types exactly match" as the final emitted type will exactly match the original type the user specified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants