VerboseLinqChain
Diagnostic Rule Overview
Field | Value |
---|---|
ID | SHIMMER2000 |
Analyzer title | Simplify LINQ chain |
Analyzer message | Replace a verbose LINQ chain with a collection expression |
Code fix title | Replace with a collection expression |
Default severity | Info |
Minimum framework/language version | C# 12 |
Category | ShimmeringStyle |
Link to code | VerboseLinqChainAnalyzer.cs |
Code fix exists? | Yes |
Detailed Explanation
Introduced C# 12, collection expressions provide a cleaner and more efficient way to construct collections compared to multiple LINQ method calls.
Examples
Flagged code:
using System.Linq;
namespace Tests;
class Test
{
static int[] array1 = [0, 1];
static int[] array2 = [5];
void Do()
{
var array3 = array1.Append(2).Prepend(3).Concat(array2).ToArray();
}
}
Fixed code:
using System.Linq;
namespace Tests;
class Test
{
static int[] array1 = [0, 1];
static int[] array2 = [5];
void Do()
{
int[] array3 = [3, .. array1, 2, .. array2];
}
}
Justification of the Severity
While using a collection expression improves readability, this is a stylistic suggestion that is not related to performance or buggy behavior.
When to Suppress
Suppress this diagnostic if you find the existing code more readable.