συμβάν
17 Μαρ, 9 μ.μ. - 21 Μαρ, 10 π.μ.
Συμμετάσχετε στη σειρά meetup για να δημιουργήσετε κλιμακούμενες λύσεις AI που βασίζονται σε πραγματικές περιπτώσεις χρήσης με συναδέλφους προγραμματιστές και ειδικούς.
Εγγραφή τώραΑυτό το πρόγραμμα περιήγησης δεν υποστηρίζεται πλέον.
Κάντε αναβάθμιση σε Microsoft Edge για να επωφεληθείτε από τις τελευταίες δυνατότητες, τις ενημερώσεις ασφαλείας και την τεχνική υποστήριξη.
Property | Value |
---|---|
Rule ID | IDE0304 |
Title | Use collection expression for builder |
Category | Style |
Subcategory | Language rules (expression-level preferences) |
Applicable languages | C# 12+ |
Options | dotnet_style_prefer_collection_expression |
This rule flags places where a CreateBuilder()
or similar method is called to create a builder type that adds elements and finally constructs a collection type that has the CollectionBuilderAttribute attribute (for example, by calling ImmutableArray<T>.Builder.ToImmutable()). Instead, a collection expression ([...]
) could be used to initialize the collection.
Σημείωση
This rule requires more recent versions of the immutable APIs (for example, System.Collections.Immutable), which opt in to the collection-expression pattern.
Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.
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. |
when_types_loosely_match (.NET 9 and later versions)* |
Prefer to use collection expressions even when types match loosely. 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 | true in .NET 8when_types_loosely_match in .NET 9 and later versions |
*The code fix when this option is used might change the semantics of your code.
// Code with violation.
var builder = ImmutableArray.CreateBuilder<int>();
builder.Add(1);
builder.AddRange(new int[] { 5, 6, 7 });
ImmutableArray<int> i = builder.ToImmutable();
// Fixed code.
ImmutableArray<int> i = [1, .. new int[] { 5, 6, 7 }];
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 IDE0304
// The code that's violating the rule is on this line.
#pragma warning restore IDE0304
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.IDE0304.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 σχόλια
.NET είναι ένα έργο ανοιχτού κώδικα. Επιλέξτε μια σύνδεση για να παρέχετε σχόλια:
συμβάν
17 Μαρ, 9 μ.μ. - 21 Μαρ, 10 π.μ.
Συμμετάσχετε στη σειρά meetup για να δημιουργήσετε κλιμακούμενες λύσεις AI που βασίζονται σε πραγματικές περιπτώσεις χρήσης με συναδέλφους προγραμματιστές και ειδικούς.
Εγγραφή τώρα