[Breaking change]: C# overload resolution prefers params
*span type* overloads which cannot be used in Expression lambdas
#43949
Labels
breaking-change
Indicates a .NET Core breaking change
doc-idea
Indicates issues that are suggestions for new topics [org][type][category]
Pri1
High priority, do before Pri2 and Pri3
source incompatible
Source code may encounter a breaking change in behavior when targeting the new version.
⌚ Not Triaged
Not triaged
Description
C# 13 adds support for
params
parameters declared with collection types other than arrays. In particular,params ReadOnlySpan<T>
andparams Span<T>
are supported, and overload resolution will prefer aparams
span type over aparams
array type when both are applicable..NET 9 adds
params
span overloads for various methods in the BCL, in addition to the existing overloads withparams
arrays. When recompiling existing callers of those methods, where arguments are passed in expanded form, the compiler will now bind to theparams
span overload.That leads to a potential breaking change for existing calls to those overloads within
Expression
lambda expressions, whereref struct
instances are not supported. In those cases, the C# 13 compiler will report an error when binding to theparams
span overload.For instance, consider
string.Join()
(see dotnet/runtime#110592):When compiled with .NET 8, the call binds to
String.Join(string? separator, params string?[] value)
, without errors.When compiled with C# 13 and .NET 9, the call binds to
String.Join(string? separator, params ReadOnlySpan<string?> value)
, and because the call is within anExpression
tree, the following errors are reported:Version
.NET 9
Previous behavior
Previously,
params
overloads were limited to array types only, so calls to those methods in expanded form resulted in implicit arrays.New behavior
With C#13 and .NET 9, for methods with overloads that take
params
array andparams
span, overload resolution will prefer theparams
span overload, resulting in an implicit span instance. For calls withinExpression
lambda expressions, the implicitref struct
span instance will be reported as an error.Type of breaking change
Reason for change
params
span support allows the compiler to avoid unnecessary allocation for theparams
argument.Recommended action
The recommended work around is to call the method with an explicit array, so the call is bound to the
params
array overload.For the example above, use
new string[] { ... }
:Feature area
Other (please put exact area in description textbox)
Affected APIs
The text was updated successfully, but these errors were encountered: