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

Merged
merged 4 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions docs/fundamentals/code-analysis/style-rules/ide0028.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "IDE0028: Use collection initializers or expressions"
description: "Learn about code analysis rule IDE0028: Use collection initializers or expressions"
ms.date: 12/12/2023
ms.date: 04/11/2025
f1_keywords:
- IDE0028
- dotnet_style_collection_initializer
Expand Down Expand Up @@ -48,23 +48,15 @@ For more information about configuring options, see [Option format](language-rul
| | `false` | Don't prefer collection initializers. |
| **Default option value** | `true` | |

### dotnet_style_prefer_collection_expression (C# only)
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **Option name** | dotnet_style_prefer_collection_expression | |
| **Option values** | `true` | Prefer to use collection expressions. |
| | `false` | Don't prefer collection expressions. |
| **Default option value** | `true` | |
(This option applies only to C#.)

## Examples

```csharp
// IDE0028 violation.
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
List<int> list = new List<int>() { 1, 2, 3 };

// Fixed code (with dotnet_style_prefer_collection_expression = true)
List<int> list = [1, 2, 3];
Expand Down
13 changes: 2 additions & 11 deletions docs/fundamentals/code-analysis/style-rules/ide0300.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,8 @@ This rule flags places where a [collection expression](../../../csharp/language-

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

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **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, `int[] i = new int[] { 1, 2, 3 };`. |
| | `when_types_loosely_match`<br />(.NET 9 and later versions)<sup>\*</sup> | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable<int> i = new int[] { 1, 2, 3 };`. 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>. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |

<sup>\*</sup>When this option is used, the code fix might change the semantics of your code. For example, if you had `IEnumerable<int> x = new int[] { 1, 2, 3 };`, then in the original code, an array is produced. But in the new code (`IEnumerable<int> x = [1, 2, 3];`), an internal compiler-synthesized type is produced instead. You can observe this difference if you use an `is` check or a cast.
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
For example, if you had `IEnumerable<int> x = new int[] { 1, 2, 3 };`, then in the original code, an array is produced. But in the new code (`IEnumerable<int> x = [1, 2, 3];`), an internal compiler-synthesized type is produced instead. You can observe this difference if you use an `is` check or a cast.

## Example

Expand Down
12 changes: 1 addition & 11 deletions docs/fundamentals/code-analysis/style-rules/ide0301.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ This rule looks for code similar to `Array.Empty<T>()` (a method call that retur

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

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **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, `int[] i = Array.Empty<int>();`. |
| | `when_types_loosely_match`<br />(.NET 9 and later versions)<sup>\*</sup> | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable<int> i = Array.Empty<int>();`. 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>. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |

<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]

## Example

Expand Down
11 changes: 3 additions & 8 deletions docs/fundamentals/code-analysis/style-rules/ide0302.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ This rule is similar to [Use collection expression for array (IDE0300)](ide0300.

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

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **Option name** | dotnet_style_prefer_collection_expression | |
| **Option values** | `true` &#124; `when_types_exactly_match` | Prefer to use collection expressions. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `true` | |
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]

The `when_types_loosely_match` value for does not apply to this rule `IDE0302`, but is listed here for completeness of the `dotnet_style_prefer_collection_expression` option (which is shared by multiple rules). The default value is effectively `true`.

## Example

Expand Down
12 changes: 1 addition & 11 deletions docs/fundamentals/code-analysis/style-rules/ide0303.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,7 @@ This rule flags places where a `Create()` method or a similar method that's desi

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

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **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, `ImmutableArray<int> i = ImmutableArray.Create(1, 2, 3);`. |
| | `when_types_loosely_match`<br />(.NET 9 and later versions)<sup>\*</sup> | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable<int> i = ImmutableArray.Create(1, 2, 3);`. 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>. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |

<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]

## Example

Expand Down
12 changes: 1 addition & 11 deletions docs/fundamentals/code-analysis/style-rules/ide0304.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,7 @@ This rule flags places where a `CreateBuilder()` or similar method is called to

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

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **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. |
| | `when_types_loosely_match`<br />(.NET 9 and later versions)<sup>\*</sup> | Prefer to use collection expressions even when types match loosely. 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>. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |

<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]

## Example

Expand Down
12 changes: 1 addition & 11 deletions docs/fundamentals/code-analysis/style-rules/ide0305.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ This rule flags places where a collection is built in a *fluent* manner, that is

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

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **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[] { 1, 2, 3 }.ToList();`. |
| | `when_types_loosely_match`<br />(.NET 9 and later versions)<sup>\*</sup> | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable<int> list = new[] { 1, 2, 3 }.ToList();`. 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>. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |

<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]

## Example

Expand Down
12 changes: 1 addition & 11 deletions docs/fundamentals/code-analysis/style-rules/ide0306.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ This rule flags places where a [collection expression](../../../csharp/language-

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

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **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> m1 = new List<int>(new[] { 1, 2, 3 });`. |
| | `when_types_loosely_match`<sup>\*</sup> | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable<int> m1 = new List<int>(new[] { 1, 2, 3 });`. 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>. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `when_types_loosely_match` | |

<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]

## Example

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
ms.topic: include
---

### dotnet_style_prefer_collection_expression

| Property | Value | Description |
|--------------------------|-------------------------------------------|---------------------------------------|
| **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>. |
| | `false` &#124; `never` | Disables the rule. |
| **Default option value** | `when_types_loosely_match`\* | |

\*When this option is used, the code fix might change the semantics of your code.
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/style-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The following table list all the code-style rules by ID and [options](../code-st
> | [IDE0025](ide0025.md) | Use expression body for properties | [csharp_style_expression_bodied_properties](ide0025.md#csharp_style_expression_bodied_properties) |
> | [IDE0026](ide0026.md) | Use expression body for indexers | [csharp_style_expression_bodied_indexers](ide0026.md#csharp_style_expression_bodied_indexers) |
> | [IDE0027](ide0027.md) | Use expression body for accessors | [csharp_style_expression_bodied_accessors](ide0027.md#csharp_style_expression_bodied_accessors) |
> | [IDE0028](ide0028.md) | Use collection initializers | [dotnet_style_collection_initializer](ide0028.md#dotnet_style_collection_initializer)<br/>[dotnet_style_prefer_collection_expression (C# only)](ide0028.md#dotnet_style_prefer_collection_expression-c-only) |
> | [IDE0028](ide0028.md) | Use collection initializers | [dotnet_style_collection_initializer](ide0028.md#dotnet_style_collection_initializer)<br/>[dotnet_style_prefer_collection_expression](ide0028.md#dotnet_style_prefer_collection_expression) |
> | [IDE0029](ide0029-ide0030-ide0270.md) | Null check can be simplified | [dotnet_style_coalesce_expression](ide0029-ide0030-ide0270.md#dotnet_style_coalesce_expression) |
> | [IDE0030](ide0029-ide0030-ide0270.md) | Null check can be simplified | [dotnet_style_coalesce_expression](ide0029-ide0030-ide0270.md#dotnet_style_coalesce_expression) |
> | [IDE0031](ide0031.md) | Use null propagation | [dotnet_style_null_propagation](ide0031.md#dotnet_style_null_propagation) |
Expand Down