🔧 Fix a List Pattern Missing a Valid Slice Case
C# list patterns let you match a collection’s shape directly ([var first, .. var rest]), but the slice pattern .. captures a contiguous middle section only when the surrounding fixed-position patterns actually leave room for it to match something — a list pattern written with more fixed positions than the input can satisfy falls through to a case that was meant to be unreachable, silently treating a valid-but-short input as the wrong shape entirely.
🔎 The Problem
int[] values = { 1, 2 };
var result = values switch
{
[var a, var b, .. var rest, var last] => $"4+: {a},{b}...{last}",
[var a, var b] => $"exactly 2: {a},{b}",
_ => "other"
};
// Looks obviously fine here, but the FIRST arm silently never
// matches a 2-element array no matter what - it needs at least 3
// elements to leave anything for the slice, and reordering arms
// in a larger match expression can hide that requirement entirely
✅ Fix: Check the Minimum Length Each Arm Actually Requires
- Count the fixed-position patterns on each side of
..in every arm — that count is the minimum number of elements the arm can ever match, and an arm requiring more elements than a shorter arm below it will never fire for inputs that satisfy only the shorter one. - Order arms from most specific (highest minimum length) to least specific, same as any other pattern-matching construct, so a later, shorter-requirement arm doesn’t accidentally shadow one that needed more elements to begin with.
- Add explicit unit tests at each arm’s minimum boundary length and one below it, since list pattern arms silently falling through to the wrong case produces no compiler warning at all.
A slice pattern needs room to slice — an arm that asks for more fixed positions than the input has will never get a turn.



