All rules
IDE0302Language rules (expression-level preferences)

Use collection expression for stackalloc

Use collection expression for stackalloc

Microsoft docs

Description

This rule is similar to Use collection expression for array (IDE0300) except it looks for stackalloc instead of arrays. Like IDE0300, it offers to convert the code to use a collection expression. For example, stackalloc int[] { ... } and stackalloc [] { ... } are simplified to [...]. This rule is only available in .NET 8 and later versions where the values can be preserved on the stack.

Example

// Code with violations.
ReadOnlySpan<int> x = stackalloc int[] { 1, 2, 3 };

// Fixed code.
ReadOnlySpan<int> x = [1, 2, 3];
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0