Редактиране

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


Use collection expression for Create() (IDE0303)

Property Value
Rule ID IDE0303
Title Use collection expression for Create
Category Style
Subcategory Language rules (expression-level preferences)
Applicable languages C# 12+
Options dotnet_style_prefer_collection_expression

Overview

This rule flags places where a Create() method or a similar method that's designated as the collection construction method (using the CollectionBuilderAttribute attribute) is used to initialize a collection and offers to replace it with a collection expression ([...]).

Create() methods are common for the immutable collections, for example, ImmutableArray.Create(1, 2, 3).

Note

This rule requires more recent versions of the immutable APIs (for example, System.Collections.Immutable), which opt in to the collection-expression pattern.

Options

Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.

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, ImmutableArray<int> i = ImmutableArray.Create(1, 2, 3);.
when_types_loosely_match
(.NET 9 and later versions)*
Prefer to use collection expressions even when types match loosely, for example, IEnumerable<int> i = ImmutableArray.Create(1, 2, 3);. 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 8
when_types_loosely_match in .NET 9 and later versions

*The code fix when this option is used might change the semantics of your code.

Example

// Code with violations.
ImmutableArray<int> i = ImmutableArray.Create(1, 2, 3);
IEnumerable<int> j = ImmutableArray.Create(1, 2, 3);

// Fixed code.
ImmutableArray<int> i = [1, 2, 3];
IEnumerable<int> j = [1, 2, 3];

The following code snippet shows an example with a custom type that's annotated with CollectionBuilderAttribute.

public class Program
{
    public static void Main()
    {
        // IDE0303 violation.
        MyCollection<int> c = MyCollection.Create(1, 2, 3);

        // IDE0303 fixed code.
        MyCollection<int> c = [1, 2, 3];
    }
}

static partial class MyCollection
{
    public static MyCollection<T> Create<T>(System.ReadOnlySpan<T> values) => default;
    public static MyCollection<T> Create<T>(T t1, T t2, T t3) => default;
}

[CollectionBuilder(typeof(MyCollection), "Create")]
class MyCollection<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator() => default;
    IEnumerator IEnumerable.GetEnumerator() => default;
}

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 IDE0303
// The code that's violating the rule is on this line.
#pragma warning restore IDE0303

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.IDE0303.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