Skip to content

Add implications on overloads for params arguments #42445

@BillWagner

Description

@BillWagner

Type of issue

Missing information

Description

Overload resolution can product different results when an argument to a params parameter is a set of discrete elements vs. a collection, especially a collection expression.

Consider:

static void M(int[] p1, params int[] p2) {} // overload 1
static void M((Span<object> p1, params ReadOnlySpan<int> p2) {} // overload 2

M([1], 2); // picks overload 1 int[] is better for p1, neither better for p2
M([1], [2]); // ambiguous int[] is better for p1, ReadOnlySpan<int> is better for p2

In this case, the conversion from collection expression is better for ReadOnlySpan. However, the same situation occurs if the second parameter were an array of int: That's an identity conversion.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#params-modifier

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/keywords/method-parameters.md

Document Version Independent Id

7241f712-8d73-b5e0-37c0-5f05c92412c6

Article author

@BillWagner

Metadata

  • ID: b8dd92fe-5053-1bc6-1c8f-cf9f88635cb5
  • Service: dotnet-csharp
  • Sub-service: lang-reference

Associated WorkItem - 320290

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions