Skip to content

Commit 62901b2

Browse files
authored
Update option values for prefer collection expressions (#45790)
1 parent 02bf9a5 commit 62901b2

File tree

10 files changed

+30
-87
lines changed

10 files changed

+30
-87
lines changed

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

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "IDE0028: Use collection initializers or expressions"
33
description: "Learn about code analysis rule IDE0028: Use collection initializers or expressions"
4-
ms.date: 12/12/2023
4+
ms.date: 04/11/2025
55
f1_keywords:
66
- IDE0028
77
- dotnet_style_collection_initializer
@@ -48,23 +48,15 @@ For more information about configuring options, see [Option format](language-rul
4848
| | `false` | Don't prefer collection initializers. |
4949
| **Default option value** | `true` | |
5050

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

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

6055
## Examples
6156

6257
```csharp
6358
// IDE0028 violation.
64-
List<int> list = new List<int>();
65-
list.Add(1);
66-
list.Add(2);
67-
list.Add(3);
59+
List<int> list = new List<int>() { 1, 2, 3 };
6860

6961
// Fixed code (with dotnet_style_prefer_collection_expression = true)
7062
List<int> list = [1, 2, 3];

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

+2-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,8 @@ This rule flags places where a [collection expression](../../../csharp/language-
2828

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

31-
### dotnet_style_prefer_collection_expression
32-
33-
| Property | Value | Description |
34-
|--------------------------|-------------------------------------------|---------------------------------------|
35-
| **Option name** | dotnet_style_prefer_collection_expression | |
36-
| **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 };`. |
37-
| | `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>. |
38-
| | `false` &#124; `never` | Disables the rule. |
39-
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
40-
41-
<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.
31+
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
32+
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.
4233

4334
## Example
4435

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,7 @@ This rule looks for code similar to `Array.Empty<T>()` (a method call that retur
2828

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

31-
### dotnet_style_prefer_collection_expression
32-
33-
| Property | Value | Description |
34-
|--------------------------|-------------------------------------------|---------------------------------------|
35-
| **Option name** | dotnet_style_prefer_collection_expression | |
36-
| **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>();`. |
37-
| | `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>. |
38-
| | `false` &#124; `never` | Disables the rule. |
39-
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
40-
41-
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
31+
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
4232

4333
## Example
4434

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ This rule is similar to [Use collection expression for array (IDE0300)](ide0300.
3131

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

34-
### dotnet_style_prefer_collection_expression
35-
36-
| Property | Value | Description |
37-
|--------------------------|-------------------------------------------|---------------------------------------|
38-
| **Option name** | dotnet_style_prefer_collection_expression | |
39-
| **Option values** | `true` &#124; `when_types_exactly_match` | Prefer to use collection expressions. |
40-
| | `false` &#124; `never` | Disables the rule. |
41-
| **Default option value** | `true` | |
34+
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
35+
36+
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`.
4237

4338
## Example
4439

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,7 @@ This rule flags places where a `Create()` method or a similar method that's desi
3333

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

36-
### dotnet_style_prefer_collection_expression
37-
38-
| Property | Value | Description |
39-
|--------------------------|-------------------------------------------|---------------------------------------|
40-
| **Option name** | dotnet_style_prefer_collection_expression | |
41-
| **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);`. |
42-
| | `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>. |
43-
| | `false` &#124; `never` | Disables the rule. |
44-
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
45-
46-
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
36+
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
4737

4838
## Example
4939

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,7 @@ This rule flags places where a `CreateBuilder()` or similar method is called to
3131

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

34-
### dotnet_style_prefer_collection_expression
35-
36-
| Property | Value | Description |
37-
|--------------------------|-------------------------------------------|---------------------------------------|
38-
| **Option name** | dotnet_style_prefer_collection_expression | |
39-
| **Option values** | `true` &#124; `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly. |
40-
| | `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>. |
41-
| | `false` &#124; `never` | Disables the rule. |
42-
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
43-
44-
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
34+
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
4535

4636
## Example
4737

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,7 @@ This rule flags places where a collection is built in a *fluent* manner, that is
2828

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

31-
### dotnet_style_prefer_collection_expression
32-
33-
| Property | Value | Description |
34-
|--------------------------|-------------------------------------------|---------------------------------------|
35-
| **Option name** | dotnet_style_prefer_collection_expression | |
36-
| **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();`. |
37-
| | `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>. |
38-
| | `false` &#124; `never` | Disables the rule. |
39-
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
40-
41-
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
31+
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
4232

4333
## Example
4434

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,7 @@ This rule flags places where a [collection expression](../../../csharp/language-
2828

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

31-
### dotnet_style_prefer_collection_expression
32-
33-
| Property | Value | Description |
34-
|--------------------------|-------------------------------------------|---------------------------------------|
35-
| **Option name** | dotnet_style_prefer_collection_expression | |
36-
| **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 });`. |
37-
| | `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>. |
38-
| | `false` &#124; `never` | Disables the rule. |
39-
| **Default option value** | `when_types_loosely_match` | |
40-
41-
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
31+
[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)]
4232

4333
## Example
4434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
ms.topic: include
3+
---
4+
5+
### dotnet_style_prefer_collection_expression
6+
7+
| Property | Value | Description |
8+
|--------------------------|-------------------------------------------|---------------------------------------|
9+
| **Option name** | dotnet_style_prefer_collection_expression | |
10+
| **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 };`. |
11+
| | `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>. |
12+
| | `false` &#124; `never` | Disables the rule. |
13+
| **Default option value** | `when_types_loosely_match`\* | |
14+
15+
\*When this option is used, the code fix might change the semantics of your code.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The following table list all the code-style rules by ID and [options](../code-st
5757
> | [IDE0025](ide0025.md) | Use expression body for properties | [csharp_style_expression_bodied_properties](ide0025.md#csharp_style_expression_bodied_properties) |
5858
> | [IDE0026](ide0026.md) | Use expression body for indexers | [csharp_style_expression_bodied_indexers](ide0026.md#csharp_style_expression_bodied_indexers) |
5959
> | [IDE0027](ide0027.md) | Use expression body for accessors | [csharp_style_expression_bodied_accessors](ide0027.md#csharp_style_expression_bodied_accessors) |
60-
> | [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) |
60+
> | [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) |
6161
> | [IDE0029](ide0029-ide0030-ide0270.md) | Null check can be simplified | [dotnet_style_coalesce_expression](ide0029-ide0030-ide0270.md#dotnet_style_coalesce_expression) |
6262
> | [IDE0030](ide0029-ide0030-ide0270.md) | Null check can be simplified | [dotnet_style_coalesce_expression](ide0029-ide0030-ide0270.md#dotnet_style_coalesce_expression) |
6363
> | [IDE0031](ide0031.md) | Use null propagation | [dotnet_style_null_propagation](ide0031.md#dotnet_style_null_propagation) |

0 commit comments

Comments
 (0)