iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪓

The Collection Expression Elimination Committee

に公開
2026/02/17

If you blindly apply collection expression suggestions to an entire project, your LINQ code might end up looking like this:

int[] values = x.Where(x => x > 0)
                .Where(x => x > 0)
                .Where(x => x > 0)
                .ToArray();

// 👇 It seems to decide whether to preserve line breaks based on the line count of the wrapping

int[] values = [.. x.Where(x => x > 0)
                .Where(x => x > 0)
                .Where(x => x > 0)];

.........In response to this, I naturally added the following to my .editorconfig:

dotnet_diagnostic.IDE0300.severity = silent   # Use collection expression for array
dotnet_diagnostic.IDE0301.severity = silent   # Use collection expression for empty
dotnet_diagnostic.IDE0305.severity = silent   # Use collection expression for fluent
dotnet_diagnostic.IDE0028.severity = silent   # Use collection initializers

But that is still not enough. Settings alone cannot eradicate existing collection expressions.

So, I introduce this!

https://github.com/sator-imaging/CollectionExpressionKiller

Five Types of Diagnostic Results

You can adjust the strictness of the eradication by combining error diagnostics.

  • CEK001: Error on all collection expressions
  • CEK002: Error if the number of elements is 4 or more
    • ✅ [1, 2, ..x]
    • ❌ [1, 2, 3, 4]
  • CEK003: Error if the expression is 12 characters or longer (including [ and ])
    • ✅ [ok]
    • ❌ [longNameVariable]
  • CEK004: Error on multi-line expressions
  • CEK005: Error on anything other than []

Example (you can apply this project-wide using .editorconfig):

values = [1234, 56789, ..values];  // Error because it is 12 characters or longer

// Settings to allow only expressions with 3 or fewer elements without line breaks
#pragma warning disable CEK001
#pragma warning disable CEK003
#pragma warning disable CEK005

values = [1234, 56789, ..values];  // OK because it's simple!
values = [1234, 56789, ..values, ..other];  // ...Maybe a bit too complex! Error!!

Basically, AI doesn't use collection expressions, but there are cases where it might fix code according to suggestions. By introducing CEK, you can prevent them from creeping in because the build will fail!

How to Disable

There may be cases where you enforce it via Directory.Build.props, but still want to use it for unit tests, for example.

◆ Disable per file

#pragma warning disable CEK001
#pragma warning disable CEK002
#pragma warning disable CEK003

int[] values = [1, 2, 3, 4, ..otherCollection];

◆ Per assembly

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage(
    "Usage",
    "CEK001:Collection expressions are not allowed",
    Justification = "Approved for this assembly")]

[assembly: SuppressMessage(
    "Usage",
    "CEK002:Collection expressions must have fewer than 4 elements",
    Justification = "Approved for this assembly")]

[assembly: SuppressMessage(
    "Usage",
    "CEK003:Collection expression text length must be 12 or fewer characters",
    Justification = "Approved for this assembly")]

◆ Per project via .editorconfig

dotnet_diagnostic.CEK001.severity = none
dotnet_diagnostic.CEK002.severity = none
dotnet_diagnostic.CEK003.severity = none

Conclusion

I have seen suggestions that come with messages like "Converting to an expression may change the context," and frankly, if it's ambiguous, they shouldn't be making the suggestion.

That's all. Thank you for your hard work.

Discussion