Skip to main content

VerboseLinqChain

Diagnostic Rule Overview

FieldValue
IDSHIMMER2000
Analyzer titleSimplify LINQ chain
Analyzer messageReplace a verbose LINQ chain with a collection expression
Code fix titleReplace with a collection expression
Default severityInfo
Minimum framework/language versionC# 12
CategoryShimmeringStyle
Link to codeVerboseLinqChainAnalyzer.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.