Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Property | Value |
---|---|
Rule ID | IDE0028 |
Title | Use collection initializers |
Category | Style |
Subcategory | Language rules (expression-level preferences) |
Applicable languages | C# and Visual Basic |
Options | dotnet_style_collection_initializer |
dotnet_style_prefer_collection_expression |
Overview
This style rule concerns the use of collection initializers and, if you're using C# 12 or later, collection expressions for collection initialization.
In .NET 8 (C# 12) and later versions, if you have the dotnet_style_prefer_collection_expression
option set to true
, the code fix in Visual Studio converts your collection initialization code to use a collection expression (List<int> list = [1, 2, 3];
). In Visual Basic and in .NET 7 (C# 11) and earlier versions, the code fix converts your code to use a collection initializer (List<int> list = new List<int> { 1, 2, 3 };
).
Note
If you use the code fix in Visual Studio, the change it offers might have different semantics in some cases. For example, int[] x = new int[] { }
is replaced with int[] x = [];
, which has slightly different semantics—the compiler uses a singleton for x
instead of creating a new instance.
Options
Set the values of the associated options for this rule to specify whether or not collection initializers and collection expressions are preferred when initializing collections.
For more information about configuring options, see Option format.
dotnet_style_collection_initializer
Property | Value | Description |
---|---|---|
Option name | dotnet_style_collection_initializer | |
Option values | true |
Prefer to use collection initializers. |
false |
Don't prefer collection initializers. | |
Default option value | true |
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<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: IEnumerable<T>, ICollection<T>, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>. |
|
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.
(This option applies only to C#.)
Examples
// IDE0028 violation.
List<int> list = new List<int>() { 1, 2, 3 };
// Fixed code (with dotnet_style_prefer_collection_expression = true)
List<int> list = [1, 2, 3];
' IDE0028 violation.
Dim list = New List(Of Integer)
list.Add(1)
list.Add(2)
list.Add(3)
' Fixed code.
Dim list = New List(Of Integer) From {1, 2, 3}
Suppress a warning
If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable IDE0028
// The code that's violating the rule is on this line.
#pragma warning restore IDE0028
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0028.severity = none
To disable all of the code-style rules, set the severity for the category Style
to none
in the configuration file.
[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-Style.severity = none
For more information, see How to suppress code analysis warnings.