Skip to content

Commit d561264

Browse files
authored
Add docs for IDE0306 (#45507)
1 parent 24fbb5a commit d561264

File tree

9 files changed

+99
-7
lines changed

9 files changed

+99
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Options specify the behavior that you want the rule to enforce. For information
3838
| | `false` | `never` | Disables the rule. |
3939
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
4040

41-
<sup>\*</sup>The code fix when this option is used 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.
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.
4242

4343
## Example
4444

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Options specify the behavior that you want the rule to enforce. For information
3838
| | `false` &#124; `never` | Disables the rule. |
3939
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
4040

41-
<sup>\*</sup>The code fix when this option is used might change the semantics of your code.
41+
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
4242

4343
## Example
4444

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Options specify the behavior that you want the rule to enforce. For information
4343
| | `false` &#124; `never` | Disables the rule. |
4444
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
4545

46-
<sup>\*</sup>The code fix when this option is used might change the semantics of your code.
46+
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
4747

4848
## Example
4949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Options specify the behavior that you want the rule to enforce. For information
4141
| | `false` &#124; `never` | Disables the rule. |
4242
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
4343

44-
<sup>\*</sup>The code fix when this option is used might change the semantics of your code.
44+
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
4545

4646
## Example
4747

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Options specify the behavior that you want the rule to enforce. For information
3838
| | `false` &#124; `never` | Disables the rule. |
3939
| **Default option value** | `true` in .NET 8<br />`when_types_loosely_match` in .NET 9 and later versions | |
4040

41-
<sup>\*</sup>The code fix when this option is used might change the semantics of your code.
41+
<sup>\*</sup>When this option is used, the code fix might change the semantics of your code.
4242

4343
## Example
4444

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: "IDE0306: Use collection expression for new"
3+
description: "Learn about code analysis rule IDE0306: Use collection expression for new"
4+
ms.date: 03/25/2025
5+
f1_keywords:
6+
- IDE0306
7+
helpviewer_keywords:
8+
- IDE0306
9+
dev_langs:
10+
- CSharp
11+
---
12+
# Use collection expression for new (IDE0306)
13+
14+
| Property | Value |
15+
|--------------------------|-----------------------------------------------|
16+
| **Rule ID** | IDE0306 |
17+
| **Title** | Use collection expression for new |
18+
| **Category** | Style |
19+
| **Subcategory** | Language rules (expression-level preferences) |
20+
| **Applicable languages** | C# 12+ |
21+
| **Options** | `dotnet_style_prefer_collection_expression` |
22+
23+
## Overview
24+
25+
This rule flags places where a [collection expression](../../../csharp/language-reference/operators/collection-expressions.md) with a spread element (`..`) could be used to initialize a list instead of `new`. For example, this rule offers to simplify code like `new List<...>(x)` into the collection expression form (`[.. x]`).
26+
27+
## Options
28+
29+
Options specify the behavior that you want the rule to enforce. For information about configuring options, see [Option format](language-rules.md#option-format).
30+
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.
42+
43+
## Example
44+
45+
```csharp
46+
// Code with violation.
47+
List<int> l1 = new List<int>(Enumerable.Range(1, 10));
48+
List<int> m1 = new List<int>(new[] { 1, 2, 3 });
49+
50+
// Fixed code.
51+
List<int> l1 = [.. Enumerable.Range(1, 10)];
52+
List<int> m1 = [.. new[] { 1, 2, 3 }];
53+
```
54+
55+
## Suppress a warning
56+
57+
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
58+
59+
```csharp
60+
#pragma warning disable IDE0306
61+
// The code that's violating the rule is on this line.
62+
#pragma warning restore IDE0306
63+
```
64+
65+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
66+
67+
```ini
68+
[*.{cs,vb}]
69+
dotnet_diagnostic.IDE0306.severity = none
70+
```
71+
72+
To disable all of the code-style rules, set the severity for the category `Style` to `none` in the [configuration file](../configuration-files.md).
73+
74+
```ini
75+
[*.{cs,vb}]
76+
dotnet_analyzer_diagnostic.category-Style.severity = none
77+
```
78+
79+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
80+
81+
## See also
82+
83+
- [Use collection expression for array (IDE0300)](ide0300.md)
84+
- [Use collection expression for empty (IDE0301)](ide0301.md)
85+
- [Use collection expression for stackalloc (IDE0302)](ide0302.md)
86+
- [Use collection expression for `Create()` (IDE0303)](ide0303.md)
87+
- [Use collection expression for builder (IDE0304)](ide0304.md)
88+
- [Use collection expression for fluent (IDE0305)](ide0305.md)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code-style rules overview
33
description: Learn about the different .NET code-style rules and categories.
4-
ms.date: 12/11/2023
4+
ms.date: 03/25/2025
55
author: gewarren
66
ms.author: gewarren
77
---
@@ -140,6 +140,7 @@ The following table list all the code-style rules by ID and [options](../code-st
140140
> | [IDE0303](ide0303.md) | Use collection expression for `Create()` | [dotnet_style_prefer_collection_expression](ide0303.md#dotnet_style_prefer_collection_expression) |
141141
> | [IDE0304](ide0304.md) | Use collection expression for builder | [dotnet_style_prefer_collection_expression](ide0304.md#dotnet_style_prefer_collection_expression) |
142142
> | [IDE0305](ide0305.md) | Use collection expression for fluent | [dotnet_style_prefer_collection_expression](ide0305.md#dotnet_style_prefer_collection_expression) |
143+
> | [IDE0306](ide0306.md) | Use collection expression for new | [dotnet_style_prefer_collection_expression](ide0306.md#dotnet_style_prefer_collection_expression) |
143144
> | [IDE0320](ide0320.md) | Make anonymous function `static` | [csharp_prefer_static_anonymous_function](ide0320.md#csharp_prefer_static_anonymous_function) |
144145
> | [IDE0330](ide0330.md) | Prefer 'System.Threading.Lock' | [csharp_prefer_system_threading_lock](ide0330.md#csharp_prefer_system_threading_lock) |
145146
> | [IDE1005](ide1005.md) | Use conditional delegate call | [csharp_style_conditional_delegate_call](ide1005.md#csharp_style_conditional_delegate_call) |

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Code-style language and unnecessary code rules
33
description: Learn about the different code-style rules for using C# and Visual Basic language constructs and for finding unnecessary code.
4-
ms.date: 11/08/2024
4+
ms.date: 03/25/2025
55
helpviewer_keywords:
66
- language code style rules [EditorConfig]
77
- language rules
@@ -168,6 +168,7 @@ C# style rules:
168168
- [Use collection expression for `Create()` (IDE0303)](ide0303.md)
169169
- [Use collection expression for builder (IDE0304](ide0304.md)
170170
- [Use collection expression for fluent (IDE0305)](ide0305.md)
171+
- [Use collection expression for new (IDE0306)](ide0306.md)
171172

172173
Visual Basic style rules:
173174

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,8 @@ items:
16691669
href: ../../fundamentals/code-analysis/style-rules/ide0304.md
16701670
- name: IDE0305
16711671
href: ../../fundamentals/code-analysis/style-rules/ide0305.md
1672+
- name: IDE0306
1673+
href: ../../fundamentals/code-analysis/style-rules/ide0306.md
16721674
- name: IDE0320
16731675
href: ../../fundamentals/code-analysis/style-rules/ide0320.md
16741676
- name: IDE0330

0 commit comments

Comments
 (0)