Skip to content

Commit e52ff75

Browse files
author
Cam Soper
committed
issue 43952
1 parent ac4aa25 commit e52ff75

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

docs/core/compatibility/10.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ If you're migrating an app to .NET 10, the breaking changes listed here might af
2121
|----------------------------------------------------------------------------------------------------------------------------|---------------------|--------------------|
2222
| [API obsoletions with non-default diagnostic IDs](core-libraries/10.0/obsolete-apis.md) | Source incompatible | Preview 1 |
2323
| [ActivitySource.CreateActivity and ActivitySource.StartActivity behavior change](core-libraries/10.0/activity-sampling.md) | Behavioral change | Preview 1 |
24+
| [C# 14 overload resolution with span parameters](csharp-overload-resolution.md) | Behavioral change | Preview 1 |
2425

2526
## Windows Forms
2627

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: "Breaking change: C# 14 overload resolution with span parameters"
3+
description: Learn about the .NET 10 breaking change in core .NET libraries where overloads with span parameters are applicable in more scenarios.
4+
ms.date: 01/30/2025
5+
---
6+
# C# 14 overload resolution with span parameters
7+
8+
C# 14 introduces new [built-in span conversions and type inference rules](https://github.com/dotnet/csharplang/issues/7905) making overloads with span parameters applicable in more scenarios.
9+
10+
## Previous behavior
11+
12+
In C# 13 and earlier, an extension method taking a `ReadOnlySpan<T>` or `Span<T>` receiver is not applicable to a value of type `T[]`. Therefore, only non-span extension methods like the ones from the `System.Linq.Enumerable` class are usually bound inside Expression lambdas.
13+
14+
## New behavior
15+
16+
In C# 14 and later, methods with `ReadOnlySpan<T>` or `Span<T>` parameters can participate in type inference or be used as extension methods in more scenarios. This makes span-based methods like the ones from the `System.MemoryExtensions` class bind in more scenarios, including inside Expression lambdas where they will cause runtime exceptions when compiled with interpretation.
17+
18+
## Version introduced
19+
20+
.NET 10 Preview 1
21+
22+
## Type of breaking change
23+
24+
This change is a [behavioral change](../../categories.md#behavioral-change).
25+
26+
## Reason for change
27+
28+
The C# language feature allows simplified API design and usage (e.g., one ReadOnlySpan extension method can apply to both spans and arrays).
29+
30+
## Recommended action
31+
32+
If you need to continue using Expression interpretation, you should make sure the non-span overloads are bound, e.g., by casting arguments to the exact types the method signature takes or calling the extension methods as explicit static invocations:
33+
34+
```cs
35+
using System;
36+
using System.Collections.Generic;
37+
using System.Linq;
38+
using System.Linq.Expressions;
39+
40+
M((array, num) => array.Contains(num)); // fails, binds to MemoryExtensions.Contains
41+
M((array, num) => ((IEnumerable<int>)array).Contains(num)); // ok, binds to Enumerable.Contains
42+
M((array, num) => array.AsEnumerable().Contains(num)); // ok, binds to Enumerable.Contains
43+
M((array, num) => Enumerable.Contains(array, num)); // ok, binds to Enumerable.Contains
44+
45+
void M(Expression<Func<int[], int, bool>> e) => e.Compile(preferInterpretation: true);
46+
```
47+
48+
## Affected APIs
49+
50+
- <xref:System.Linq.Expressions.Expression`1.Compile*?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ items:
1414
href: core-libraries/10.0/obsolete-apis.md
1515
- name: ActivitySource.CreateActivity and ActivitySource.StartActivity behavior change
1616
href: core-libraries/10.0/activity-sampling.md
17+
- name: C# 14 overload resolution with span parameters
18+
href: csharp-overload-resolution.md
1719
- name: Windows Forms
1820
items:
1921
- name: TreeView checkbox image truncation
@@ -1320,6 +1322,8 @@ items:
13201322
href: core-libraries/10.0/obsolete-apis.md
13211323
- name: ActivitySource.CreateActivity and ActivitySource.StartActivity behavior change
13221324
href: core-libraries/10.0/activity-sampling.md
1325+
- name: C# 14 overload resolution with span parameters
1326+
href: csharp-overload-resolution.md
13231327
- name: .NET 9
13241328
items:
13251329
- name: Adding a ZipArchiveEntry sets header general-purpose bit flags

0 commit comments

Comments
 (0)