C# 14 first-class spans can change which overload you call

Retarget a project to net10.0 and the compiler moves to C# 14 with it. One C# 14 feature, first-class spans, adds implicit conversions from T[] to Span<T> and ReadOnlySpan<T>. That makes span overloads applicable in calls where C# 13 never considered them. Most of the time you get a faster overload for free. Sometimes the same source line stops compiling, and in one case it compiles fine and throws ArrayTypeMismatchException at runtime.
We reproduced every documented C# 14 span break on the .NET 10 SDK by building the same source with LangVersion 13 and 14. This post covers the new conversion rules, five concrete breaks, the call-site and API-author fixes, and how to audit a codebase before the upgrade. It does not cover the other C# 14 features (extension members, field, null-conditional assignment) or the performance side of spans.
What changed: arrays now convert to spans in overload resolution
Before C# 14, Span<T> and ReadOnlySpan<T> had user-defined implicit operators from T[]. User-defined conversions don’t take part in generic type inference, and they aren’t allowed on an extension-method receiver. So a call like Write(numbers) with a Write<T>(ReadOnlySpan<T>) overload couldn’t infer T from int[], and numbers.Contains(2) never bound to MemoryExtensions.Contains.
The C# 14 first-class span types specification adds a language-level implicit span conversion:
- from a single-dimensional array
E[]toSpan<E>, - from
E[]toReadOnlySpan<U>whenEis covariance-convertible toU, - from
Span<T>toReadOnlySpan<U>, and fromReadOnlySpan<T>toReadOnlySpan<U>, with the same covariance rule, - from
stringtoReadOnlySpan<char>.
These are standard implicit conversions, so they work for type inference and for extension receivers. The spec also adds two tie-break rules, and the tie-break rules decide which method actually runs:
- A span conversion beats a non-span conversion when the argument exactly matches neither parameter type.
int[]toReadOnlySpan<int>beatsint[]toIEnumerable<int>. ReadOnlySpan<T>is a better conversion target thanSpan<T>. When both span overloads are applicable, the read-only one wins.
How the compiler picks among three Write overloads for an int[] argument. Rules from the first-class span types specification.
What’s new in C# 14 says C# 14 is supported on .NET 10, and a project targeting net10.0 gets C# 14 by default. You can confirm this with an empty project: the first sample below behaves as C# 14 with no LangVersion set.
Test setup: one source tree, two language versions
Every sample is a small console app. A shared Directory.Build.props lets you switch the language version from the command line:
<Project>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- Override from the command line: dotnet run -p:LangVersion=13 -->
<LangVersion Condition="'$(LangVersion)' == ''">14</LangVersion>
</PropertyGroup>
</Project>
Environment: .NET SDK 10.0.402 with the 10.0.13 runtime, plus the 9.0.21 runtime for the net9.0 sample, on Linux x64. Each program ran under dotnet run -p:LangVersion=13 and dotnet run -p:LangVersion=14; the compile-only cases used dotnet build. The outputs below are copied from those runs. Download the projects and a run.sh that runs them all: csharp-14-span-overloads-sample.zip.
Break 1: the same call silently binds to a different overload
This is the change you will hit most and notice least:
int[] numbers = [3, 1, 2];
Sink.Write(numbers);
static class Sink
{
public static void Write<T>(IEnumerable<T> items) => Console.WriteLine("Write(IEnumerable<T>)");
public static void Write<T>(ReadOnlySpan<T> items) => Console.WriteLine("Write(ReadOnlySpan<T>)");
}
C# 13: Write(IEnumerable<T>)
C# 14: Write(ReadOnlySpan<T>)
No warning, no error. If both overloads do the same thing, this is the point of the feature: the span overload usually avoids an enumerator. If your two overloads have different semantics (one copies and one doesn’t, one logs and one doesn’t, one is lazy), behavior changes on recompile. The same applies to the BCL: array.Contains(x) and similar calls can now bind to MemoryExtensions instead of Enumerable.
Break 2: covariant arrays throw ArrayTypeMismatchException
C# arrays are covariant: a string[] can sit in an object[] variable. Span<T> is not covariant, and its constructor throws when the array’s real element type differs from T. The Roslyn breaking-changes list for .NET 10 documents this case:
object[] items = new string[] { "a" };
try { Covariant.R(items); }
catch (ArrayTypeMismatchException ex) { Console.WriteLine($"R(object[]) threw {ex.GetType().Name}"); }
static class Covariant
{
public static void R<T>(IEnumerable<T> e) => Console.WriteLine("R(IEnumerable<T>)");
public static void R<T>(Span<T> s) => Console.WriteLine("R(Span<T>)");
}
C# 13: R(IEnumerable<T>)
C# 14: R(object[]) threw ArrayTypeMismatchException
The code compiles under both versions. Only a test that passes a covariant array finds it. This failure mode is the reason rule 2 exists: an array converts to ReadOnlySpan<T> through the covariance-aware conversion, so adding a ReadOnlySpan<T> overload makes the call safe again (see the fixes below).
Break 3: calls that were unambiguous become CS0121
When a method has an array overload and a mixed-span overload, the new conversions can leave the compiler with no single best candidate. The Roslyn document shows this with xUnit-style Assert.Equal overloads; our repro uses a local type with the same shape:
long[] x = [1];
MyAssert.Equal([2], x);
static class MyAssert
{
public static void Equal<T>(T[] expected, T[] actual) => Console.WriteLine("Equal(T[], T[])");
public static void Equal<T>(ReadOnlySpan<T> expected, Span<T> actual) => Console.WriteLine("Equal(ReadOnlySpan<T>, Span<T>)");
}
C# 13: Build succeeded.
C# 14: error CS0121: The call is ambiguous between the following methods or properties:
'MyAssert.Equal<T>(T[], T[])' and 'MyAssert.Equal<T>(System.ReadOnlySpan<T>, System.Span<T>)'
The collection expression [2] prefers the span parameter, and the array x exactly matches T[]. Each overload is better for one argument, so neither wins.
Break 4: MemoryMarshal.Cast now returns ReadOnlySpan
This one uses the real BCL, no custom types. MemoryMarshal.Cast has a Span<TFrom> → Span<TTo> overload and a ReadOnlySpan<TFrom> → ReadOnlySpan<TTo> overload:
using System.Runtime.InteropServices;
double[] d = new double[4];
Span<ulong> bits = MemoryMarshal.Cast<double, ulong>(d);
C# 13: Build succeeded.
C# 14: error CS0029: Cannot implicitly convert type 'System.ReadOnlySpan<ulong>' to 'System.Span<ulong>'
In C# 13, the array reached the Span overload through the user-defined operator. In C# 14 both overloads are applicable through span conversions, rule 2 picks ReadOnlySpan<double>, and the result no longer fits in a Span<ulong>. Writable reinterpret casts over arrays are common in parsing and numeric code, so expect this one in low-level libraries.
Break 5: expression trees bind to MemoryExtensions and fail when interpreted
Expression trees record the method the compiler bound. In C# 14, array.Contains(n) inside an Expression<...> records MemoryExtensions.Contains, which takes a ReadOnlySpan<T>. The .NET 10 breaking change for span overload resolution says this causes runtime exceptions when the tree is compiled with interpretation:
using System.Linq.Expressions;
Expression<Func<int[], int, bool>> e = (array, n) => array.Contains(n);
Console.WriteLine($"Contains binds to {((MethodCallExpression)e.Body).Method.DeclaringType!.Name}");
var f = e.Compile(preferInterpretation: true);
Console.WriteLine($"Interpreted Contains returned {f([3, 1, 2], 2)}");
C# 13: Contains binds to Enumerable
Interpreted Contains returned True
C# 14: Contains binds to MemoryExtensions
Interpreted Contains threw ArgumentException: GenericArguments[1], 'System.ReadOnlySpan`1[System.Int32]', on
'System.Linq.Expressions.Interpreter.FuncCallInstruction`2[T0,TRet]' violates the constraint of type 'TRet'.
The interpreter can’t hold a ref struct like ReadOnlySpan<int> as an intermediate value. Any library that walks expression trees (query providers, rule engines, mapping libraries) will also see a method it may not expect. Test the queries in your own stack that call Contains or other LINQ methods on arrays inside expressions.
Bonus: Enumerable.Reverse on targets older than net10.0
C# 14 on net9.0 or older is an unsupported configuration, but LangVersion=latest makes it easy to end up there. The Roslyn document describes what happens to Reverse:
int[] x = [1, 2, 3];
x.Reverse();
Console.WriteLine(string.Join(",", x));
net9.0, C# 13: 1,2,3
net9.0, C# 14: 3,2,1
net10.0, C# 14: 1,2,3
On net9.0, C# 14 binds MemoryExtensions.Reverse(Span<T>), which reverses the array in place and returns void, instead of the lazy Enumerable.Reverse. .NET 10 added Enumerable.Reverse<T>(this T[]), which is an exact match, so net10.0 keeps the old behavior. If you multi-target, pin LangVersion per target framework or call Enumerable.Reverse(x) explicitly.
Fixing call sites: say which conversion you mean
At the call site, make the argument’s type exact so the compiler has nothing to choose between:
MyAssert.Equal([2], x.AsSpan()); // Break 3
Span<ulong> bits = MemoryMarshal.Cast<double, ulong>(d.AsSpan()); // Break 4
Covariant.R(items.AsEnumerable()); // Break 2
Expression<Func<int[], int, bool>> e =
(array, n) => Enumerable.Contains(array, n); // Break 5
The Fixed sample runs all four. It prints the same output under C# 13 and C# 14: Equal(ReadOnlySpan<T>, Span<T>), 4, R(IEnumerable<T>) and True. An .AsSpan() call costs nothing extra: the array-to-span conversion does the same work.
Fixing APIs: give the compiler a winner
If you maintain a library, your users will hit these breaks without changing their code. You have two tools.
Add a ReadOnlySpan<T> overload next to an existing Span<T> overload when the method doesn’t write. Rule 2 makes it win, and the covariance-aware conversion makes it safe for covariant arrays:
static class Covariant
{
public static void R<T>(IEnumerable<T> e) => Console.WriteLine("R(IEnumerable<T>)");
public static void R<T>(Span<T> s) => Console.WriteLine("R(Span<T>)");
// ReadOnlySpan<T> beats Span<T> and accepts covariant arrays safely.
public static void R<T>(ReadOnlySpan<T> s) => Console.WriteLine("R(ReadOnlySpan<T>)");
}
C# 13: R(IEnumerable<T>)
C# 14: R(ReadOnlySpan<T>)
Use OverloadResolutionPriorityAttribute (C# 13, .NET 9+) to keep an existing overload in front. Overload resolution only compares priorities among candidates in the same type, and higher wins:
using System.Runtime.CompilerServices;
static class MyAssert
{
[OverloadResolutionPriority(1)]
public static void Equal<T>(T[] expected, T[] actual) => Console.WriteLine("Equal(T[], T[])");
public static void Equal<T>(ReadOnlySpan<T> expected, Span<T> actual) => Console.WriteLine("Equal(ReadOnlySpan<T>, Span<T>)");
}
With the attribute, MyAssert.Equal([2], x) compiles under C# 14 and calls Equal(T[], T[]), as it did in C# 13. The C# 13 notes on overload resolution priority and the attribute reference cover the rules. The trade-off: priority overrides normal betterness entirely within that type, so a high priority on a slow overload also hides a faster one from callers who would have benefited.
Auditing a codebase before you retarget
The compile errors announce themselves. The silent rebinding in breaks 1, 2 and 5 doesn’t, and there is no compiler warning for “this call binds differently than in C# 13”. What we found useful:
- Build once with
-p:LangVersion=13onnet10.0to separate the runtime upgrade from the language upgrade. Fix runtime breaks first, then flip to C# 14 and fix CS0121 and CS0029 errors. - Search your own APIs for overload pairs that mix
T[]orIEnumerable<T>withSpan<T>orReadOnlySpan<T>. Check that each pair has the same semantics, and add aReadOnlySpan<T>overload wherever onlySpan<T>exists for read-only work. - Search for covariant array use (
object[] x = new string[...],params object[]forwarding) that reaches span-accepting methods, and add a test for it. - Run tests that compile expression trees, especially anything using
Compile(preferInterpretation: true)or a custom LINQ provider. - Check multi-targeted projects for
LangVersion=latestcombined with a TFM older thannet10.0, and fixReversecalls on arrays.
When to keep C# 13 instead
Pinning <LangVersion>13</LangVersion> on a net10.0 project is a legitimate short-term choice for a large codebase. It lets you take the .NET 10 runtime and library changes now and the language change later. It also means giving up the rest of C# 14 and the faster span overloads, so treat it as a migration step with an end date rather than a setting to keep.
Takeaways
- A language upgrade can change which method runs without changing a line of source. Overload resolution is part of your public API surface.
- When a type has both
Span<T>andReadOnlySpan<T>overloads, C# 14 prefersReadOnlySpan<T>. Libraries should make sure that overload exists and does the right thing. - Covariant arrays and spans don’t mix. A passing build proves nothing about that case; only a test does.
- Separate the runtime upgrade from the language upgrade when you retarget, so each failure has one cause.
Download the sample projects, run ./run.sh on a machine with the .NET 10 SDK, then build your own solution with -p:LangVersion=13 and -p:LangVersion=14 and compare the errors. The full rule set is in the first-class span types specification.
