Događaji
Izgradite inteligentne aplikacije
M03 17 21 - M03 21 10
Pridružite se seriji susreta kako biste sa kolegama programerima i stručnjacima izgradili skalabilna AI rješenja zasnovana na stvarnim slučajevima korištenja.
Registrirajte seOvaj preglednik više nije podržan.
Nadogradite na Microsoft Edge da iskoristite najnovije osobine, sigurnosna ažuriranja i tehničku podršku.
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 |
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 };
).
Bilješka
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.
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.
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 |
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 |
// 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}
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.
.NET povratne informacije
.NET je projekat otvorenog koda. Odaberite vezu za pružanje povratnih informacija:
Događaji
Izgradite inteligentne aplikacije
M03 17 21 - M03 21 10
Pridružite se seriji susreta kako biste sa kolegama programerima i stručnjacima izgradili skalabilna AI rješenja zasnovana na stvarnim slučajevima korištenja.
Registrirajte se