🔧 Fix a Params Collection Overload Picked Over a Span One
Extending params support beyond arrays to any collection type, including Span<T> and ReadOnlySpan<T>, came with a deliberate overload-resolution preference: when a params array overload and a params span overload are both applicable to a call, the compiler prefers the span-based one, to favor the allocation-free path. That preference is invisible right up until a span-based params overload is added next to an existing array-based one purely for performance – at which point every existing call site that used to bind to the array overload can silently rebind to the new one instead, with no change at all to the call site’s own source.
🔎 The Problem
public static int Sum(params int[] values)
{
Console.WriteLine("array overload");
return values.Sum();
}
public static int Sum(params ReadOnlySpan<int> values)
{
Console.WriteLine("span overload"); // added later for performance
int total = 0;
foreach (var v in values) total += v;
return total;
}
Sum(1, 2, 3);
// Before the span overload existed: always bound to the array overload
// After adding it: binds to the SPAN overload instead, because the
// language prefers a params Span/ReadOnlySpan overload over a params
// array overload when both apply equally well - any code relying on
// the array overload's own side effect (a log line, a different
// telemetry path) silently stops firing
✅ Fix: Audit Call Sites Before Adding the Overload
- Before adding a span-based params overload next to an existing array-based one purely for a performance win, search for every call site and confirm none of them depend on array-overload-specific behavior – a side effect only that version performs, or reflection over the parameter as an array.
- If both overloads genuinely need to coexist with different behavior, give them different names instead of relying on overload resolution’s span-over-array preference to route calls correctly – a preference rule that favors one overload silently is much easier to get right by simply not needing it.
- Add a regression test asserting which overload a handful of representative call sites bind to, by checking for each overload’s own observable side effect, before and after introducing the new overload, so a change in binding is caught by CI instead of discovered as a quiet behavior change in production.
🚨 Why This Is Dangerous
Overload resolution changing which method a call site binds to, with the call site’s own source code completely unchanged, is one of the hardest categories of bug to find by reading a diff – the only line that changed was somewhere else in the file, adding a new overload that nobody realized would quietly steal every existing call.
Adding an overload is never purely additive once params and span preference rules are involved – every existing call to that method name is a candidate for silently rebinding to the new one.




