Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Params Collection Overload Picked Over a Span One

- 28.11.26 - ErcanOPAK

🔧 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.

— C# Best Practice

Related posts:

@helpers in Asp.NET MVC Razor

@helpers in Asp.NET MVC Razor

C#: Use Using Static to Call Math Methods Without Class Name

C#: Use Using Static to Call Math Methods Without Class Name

C#: Why a Static Constructor Runs at a Time You Didn't Expect

C#: Why a Static Constructor Runs at a Time You Didn't Expect

Views: 8
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Kubernetes: Fix a StatefulSet Stuck Waiting on Ordinal Zero
HTML5: Fix a Fieldset Disabled State Leaking Into a Nested Form

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com