From 3a09cab998c4b8910e0d31c4d19062150e672da3 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 11 Apr 2025 16:22:40 -0700 Subject: [PATCH 1/4] update option values for prefer collection expr --- .../code-analysis/style-rules/ide0028.md | 16 ++++++++-------- .../code-analysis/style-rules/ide0300.md | 4 ++-- .../code-analysis/style-rules/ide0301.md | 4 ++-- .../code-analysis/style-rules/ide0302.md | 7 +++++-- .../code-analysis/style-rules/ide0303.md | 4 ++-- .../code-analysis/style-rules/ide0304.md | 4 ++-- .../code-analysis/style-rules/ide0305.md | 4 ++-- 7 files changed, 23 insertions(+), 20 deletions(-) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0028.md b/docs/fundamentals/code-analysis/style-rules/ide0028.md index b5aab6971bbe2..ec490cb0d4079 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0028.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0028.md @@ -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 @@ -53,18 +53,18 @@ For more information about configuring options, see [Option format](language-rul | 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` | | +| **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `List list = new List() { 1, 2, 3 };`. | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable list = new List() { 1, 2, 3 };`. The targeted type must match the type on the right-hand side or be one of the following types: , , , , . | +| | `false` | `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. ## Examples ```csharp // IDE0028 violation. -List list = new List(); -list.Add(1); -list.Add(2); -list.Add(3); +List list = new List() { 1, 2, 3 }; // Fixed code (with dotnet_style_prefer_collection_expression = true) List list = [1, 2, 3]; diff --git a/docs/fundamentals/code-analysis/style-rules/ide0300.md b/docs/fundamentals/code-analysis/style-rules/ide0300.md index e1c11adc2f31e..ed0465ec1fc6e 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0300.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0300.md @@ -34,9 +34,9 @@ Options specify the behavior that you want the rule to enforce. For information |--------------------------|-------------------------------------------|---------------------------------------| | **Option name** | dotnet_style_prefer_collection_expression | | | **Option values** | `true` | `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`
(.NET 9 and later versions)\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | | | `false` | `never` | Disables the rule. | -| **Default option value** | `true` in .NET 8
`when_types_loosely_match` in .NET 9 and later versions | | +| **Default option value** | `when_types_loosely_match` | | \*When this option is used, the code fix might change the semantics of your code. For example, if you had `IEnumerable x = new int[] { 1, 2, 3 };`, then in the original code, an array is produced. But in the new code (`IEnumerable 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. diff --git a/docs/fundamentals/code-analysis/style-rules/ide0301.md b/docs/fundamentals/code-analysis/style-rules/ide0301.md index 94c333a5395f7..44d8ff78e5f6b 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0301.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0301.md @@ -34,9 +34,9 @@ Options specify the behavior that you want the rule to enforce. For information |--------------------------|-------------------------------------------|---------------------------------------| | **Option name** | dotnet_style_prefer_collection_expression | | | **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `int[] i = Array.Empty();`. | -| | `when_types_loosely_match`
(.NET 9 and later versions)\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable i = Array.Empty();`. The targeted type must match the type on the right-hand side or be one of the following types: , , , , . | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable i = Array.Empty();`. The targeted type must match the type on the right-hand side or be one of the following types: , , , , . | | | `false` | `never` | Disables the rule. | -| **Default option value** | `true` in .NET 8
`when_types_loosely_match` in .NET 9 and later versions | | +| **Default option value** | `when_types_loosely_match` | | \*When this option is used, the code fix might change the semantics of your code. diff --git a/docs/fundamentals/code-analysis/style-rules/ide0302.md b/docs/fundamentals/code-analysis/style-rules/ide0302.md index 33128b9bcbbf7..651af5ccdf683 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0302.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0302.md @@ -36,9 +36,12 @@ Options specify the behavior that you want the rule to enforce. For information | Property | Value | Description | |--------------------------|-------------------------------------------|---------------------------------------| | **Option name** | dotnet_style_prefer_collection_expression | | -| **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions. | +| **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly. | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely. | | | `false` | `never` | Disables the rule. | -| **Default option value** | `true` | | +| **Default option value** | `when_types_loosely_match`\* | | + +\*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 diff --git a/docs/fundamentals/code-analysis/style-rules/ide0303.md b/docs/fundamentals/code-analysis/style-rules/ide0303.md index 0ff0bb5551956..e00f600fa6e34 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0303.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0303.md @@ -39,9 +39,9 @@ Options specify the behavior that you want the rule to enforce. For information |--------------------------|-------------------------------------------|---------------------------------------| | **Option name** | dotnet_style_prefer_collection_expression | | | **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `ImmutableArray i = ImmutableArray.Create(1, 2, 3);`. | -| | `when_types_loosely_match`
(.NET 9 and later versions)\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | | | `false` | `never` | Disables the rule. | -| **Default option value** | `true` in .NET 8
`when_types_loosely_match` in .NET 9 and later versions | | +| **Default option value** | `when_types_loosely_match` | | \*When this option is used, the code fix might change the semantics of your code. diff --git a/docs/fundamentals/code-analysis/style-rules/ide0304.md b/docs/fundamentals/code-analysis/style-rules/ide0304.md index aad305a9327db..b720243627be5 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0304.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0304.md @@ -37,9 +37,9 @@ Options specify the behavior that you want the rule to enforce. For information |--------------------------|-------------------------------------------|---------------------------------------| | **Option name** | dotnet_style_prefer_collection_expression | | | **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly. | -| | `when_types_loosely_match`
(.NET 9 and later versions)\* | 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: , , , , . | +| | `when_types_loosely_match`\* | 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: , , , , . | | | `false` | `never` | Disables the rule. | -| **Default option value** | `true` in .NET 8
`when_types_loosely_match` in .NET 9 and later versions | | +| **Default option value** | `when_types_loosely_match` | | \*When this option is used, the code fix might change the semantics of your code. diff --git a/docs/fundamentals/code-analysis/style-rules/ide0305.md b/docs/fundamentals/code-analysis/style-rules/ide0305.md index 9fe3720909826..7d8caaa1ec9e6 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0305.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0305.md @@ -34,9 +34,9 @@ Options specify the behavior that you want the rule to enforce. For information |--------------------------|-------------------------------------------|---------------------------------------| | **Option name** | dotnet_style_prefer_collection_expression | | | **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `List list = new[] { 1, 2, 3 }.ToList();`. | -| | `when_types_loosely_match`
(.NET 9 and later versions)\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | | | `false` | `never` | Disables the rule. | -| **Default option value** | `true` in .NET 8
`when_types_loosely_match` in .NET 9 and later versions | | +| **Default option value** | `when_types_loosely_match` | | \*When this option is used, the code fix might change the semantics of your code. From cf8f7a26c8c71bcc176170c65681b099c7eac284 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 11 Apr 2025 17:10:22 -0700 Subject: [PATCH 2/4] put option in include file --- .../code-analysis/style-rules/ide0028.md | 12 +----------- .../code-analysis/style-rules/ide0300.md | 13 ++----------- .../code-analysis/style-rules/ide0301.md | 12 +----------- .../code-analysis/style-rules/ide0302.md | 12 ++---------- .../code-analysis/style-rules/ide0303.md | 12 +----------- .../code-analysis/style-rules/ide0304.md | 12 +----------- .../code-analysis/style-rules/ide0305.md | 10 +--------- .../code-analysis/style-rules/ide0306.md | 12 +----------- .../dotnet-style-prefer-collection-expression.md | 15 +++++++++++++++ 9 files changed, 25 insertions(+), 85 deletions(-) create mode 100644 docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md diff --git a/docs/fundamentals/code-analysis/style-rules/ide0028.md b/docs/fundamentals/code-analysis/style-rules/ide0028.md index ec490cb0d4079..a20d836c99a2f 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0028.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0028.md @@ -48,17 +48,7 @@ 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) - -| Property | Value | Description | -|--------------------------|-------------------------------------------|---------------------------------------| -| **Option name** | dotnet_style_prefer_collection_expression | | -| **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `List list = new List() { 1, 2, 3 };`. | -| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable list = new List() { 1, 2, 3 };`. The targeted type must match the type on the right-hand side or be one of the following types: , , , , . | -| | `false` | `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. +[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] ## Examples diff --git a/docs/fundamentals/code-analysis/style-rules/ide0300.md b/docs/fundamentals/code-analysis/style-rules/ide0300.md index ed0465ec1fc6e..d396b44b115e5 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0300.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0300.md @@ -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` | `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`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | -| | `false` | `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. For example, if you had `IEnumerable x = new int[] { 1, 2, 3 };`, then in the original code, an array is produced. But in the new code (`IEnumerable 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 x = new int[] { 1, 2, 3 };`, then in the original code, an array is produced. But in the new code (`IEnumerable 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 diff --git a/docs/fundamentals/code-analysis/style-rules/ide0301.md b/docs/fundamentals/code-analysis/style-rules/ide0301.md index 44d8ff78e5f6b..a294f5112c398 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0301.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0301.md @@ -28,17 +28,7 @@ This rule looks for code similar to `Array.Empty()` (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` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `int[] i = Array.Empty();`. | -| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable i = Array.Empty();`. The targeted type must match the type on the right-hand side or be one of the following types: , , , , . | -| | `false` | `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. +[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] ## Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0302.md b/docs/fundamentals/code-analysis/style-rules/ide0302.md index 651af5ccdf683..9856608df69ee 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0302.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0302.md @@ -31,17 +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 +[!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` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly. | -| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely. | -| | `false` | `never` | Disables the rule. | -| **Default option value** | `when_types_loosely_match`\* | | - -\*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`. +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 diff --git a/docs/fundamentals/code-analysis/style-rules/ide0303.md b/docs/fundamentals/code-analysis/style-rules/ide0303.md index e00f600fa6e34..f4c3e7ac93b1a 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0303.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0303.md @@ -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` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `ImmutableArray i = ImmutableArray.Create(1, 2, 3);`. | -| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | -| | `false` | `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. +[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] ## Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0304.md b/docs/fundamentals/code-analysis/style-rules/ide0304.md index b720243627be5..62429b34d1215 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0304.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0304.md @@ -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` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly. | -| | `when_types_loosely_match`\* | 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: , , , , . | -| | `false` | `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. +[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] ## Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0305.md b/docs/fundamentals/code-analysis/style-rules/ide0305.md index 7d8caaa1ec9e6..da7ea32515c49 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0305.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0305.md @@ -30,15 +30,7 @@ Options specify the behavior that you want the rule to enforce. For information ### dotnet_style_prefer_collection_expression -| Property | Value | Description | -|--------------------------|-------------------------------------------|---------------------------------------| -| **Option name** | dotnet_style_prefer_collection_expression | | -| **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `List list = new[] { 1, 2, 3 }.ToList();`. | -| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | -| | `false` | `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. +[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] ## Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0306.md b/docs/fundamentals/code-analysis/style-rules/ide0306.md index 8640ed6b7a9d8..cdd28c8e35313 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0306.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0306.md @@ -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` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `List m1 = new List(new[] { 1, 2, 3 });`. | -| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable m1 = new List(new[] { 1, 2, 3 });`. The targeted type must match the type on the right-hand side or be one of the following types: , , , , . | -| | `false` | `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. +[!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] ## Example diff --git a/docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md b/docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md new file mode 100644 index 0000000000000..4caabffae4a19 --- /dev/null +++ b/docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md @@ -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` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `ImmutableArray i = ImmutableArray.Create(1, 2, 3);`. | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | +| | `false` | `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. From cfd4953aaa4bea94e1e18b62218a2d4f69e4391e Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 11 Apr 2025 18:39:16 -0700 Subject: [PATCH 3/4] fixes --- docs/fundamentals/code-analysis/style-rules/ide0305.md | 2 -- .../includes/dotnet-style-prefer-collection-expression.md | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0305.md b/docs/fundamentals/code-analysis/style-rules/ide0305.md index da7ea32515c49..77f54e167bacb 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0305.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0305.md @@ -28,8 +28,6 @@ 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 - [!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] ## Example diff --git a/docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md b/docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md index 4caabffae4a19..177fef32f4d61 100644 --- a/docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md +++ b/docs/fundamentals/code-analysis/style-rules/includes/dotnet-style-prefer-collection-expression.md @@ -7,8 +7,8 @@ ms.topic: include | Property | Value | Description | |--------------------------|-------------------------------------------|---------------------------------------| | **Option name** | dotnet_style_prefer_collection_expression | | -| **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `ImmutableArray i = ImmutableArray.Create(1, 2, 3);`. | -| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable 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: , , , , . | +| **Option values** | `true` | `when_types_exactly_match` | Prefer to use collection expressions only when types match exactly, for example, `List list = new List() { 1, 2 };`. | +| | `when_types_loosely_match`\* | Prefer to use collection expressions even when types match loosely, for example, `IEnumerable list = new List() { 1, 2 };`. The targeted type must match the type on the right-hand side or be one of the following types: , , , , . | | | `false` | `never` | Disables the rule. | | **Default option value** | `when_types_loosely_match`\* | | From 6511c5a59832b9af428f1f5962b235908aa0862e Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 11 Apr 2025 18:46:06 -0700 Subject: [PATCH 4/4] fix bookmark --- docs/fundamentals/code-analysis/style-rules/ide0028.md | 2 ++ docs/fundamentals/code-analysis/style-rules/index.md | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0028.md b/docs/fundamentals/code-analysis/style-rules/ide0028.md index a20d836c99a2f..412a04a23430a 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0028.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0028.md @@ -50,6 +50,8 @@ For more information about configuring options, see [Option format](language-rul [!INCLUDE [dotnet-style-prefer-collection-expression](includes/dotnet-style-prefer-collection-expression.md)] +(This option applies only to C#.) + ## Examples ```csharp diff --git a/docs/fundamentals/code-analysis/style-rules/index.md b/docs/fundamentals/code-analysis/style-rules/index.md index ee1e7693b6dcf..adc1758116ae2 100644 --- a/docs/fundamentals/code-analysis/style-rules/index.md +++ b/docs/fundamentals/code-analysis/style-rules/index.md @@ -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)
[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)
[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) |