Редактиране

Споделяне чрез


Use collection initializers or expressions (IDE0028)

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 fixer 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 fixer converts your code to use a collection initializer (List<int> list = new List<int> { 1, 2, 3 };).

Note

If you use the code fixer 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 (C# only)

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

Examples

// IDE0028 violation.
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(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}
// IDE0028 violation.
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);

// Fixed code.
List<int> list = new List<int>
{
    1,
    2,
    3
};
// IDE0028 violation.
List<int> list = new List<int>();
list.Add(1);
list.AddRange(new[] { 5, 6, 7 });

// Fixed code.
List<int> list = [1, .. new[] { 5, 6, 7 }];
' 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.

See also