|
| 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` | `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` | `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) |
0 commit comments