ReSharper 2025.3 Help

Code inspection: C# 14 breaking change in overload resolution with span parameters

C# 14 introduces new built-in span conversions and type inference rules. It makes overloads with span parameters applicable in more scenarios but brings some breaking changes.

ReSharper identifies places that might be affected by these breaking changes and suggests reviewing them. Not all occurrences of the changed behavior cause errors during compilation or at runtime, so there is no need to fix all suggestions. It is recommended to disable this inspection after reviewing all occurrences and fixing those that could cause errors during compilation or at runtime.

Previous behavior

In C# prior to C# 14, extension methods with ReadOnlySpan<T> or Span<T> parameters could not be applied directly to arrays of type T[]. As a result, when working with arrays, the compiler would only select non-span extension methods (such as those defined in the System.Linq.Enumerable class) during method resolution.

New behavior

Starting with C# 14, methods that accept ReadOnlySpan<T> or Span<T> parameters have expanded capabilities for type inference and can serve as extension methods in a wider range of contexts. As a consequence, span-based methods found in libraries like System.MemoryExtensions are now applicable in additional scenarios where they might unexpectedly trigger runtime exceptions.

Examples

ArrayTypeMismatchException at runtime when selecting an overload with Span<T> for a covariant array:

string[] strings = new[] { "a" }; M(strings); void M(object[] possibleCovariantArray) { // ok with C# 13 because it uses overload with IEnumerable<T>, // but in C# 14, ArrayTypeMismatchException is thrown at runtime // when converting a covariant array to Span<object> Util.DoSomething(possibleCovariantArray); } static class Util { public static void DoSomething<T>(IEnumerable<T> e) => Console.Write("IEnumerable<T>"); public static void DoSomething<T>(Span<T> s) => Console.Write("Span<T>"); }

Compilation error because of changed overload resolution:

string[] strings = new[] { "a" }; // C# 13 uses overload with IEnumerable<T>, which has a return type, // but C# 14 uses overload with Span<T>, which has no return type _ = strings.Reverse(); static class Util { public static IEnumerable<T> Reverse<T>(this IEnumerable<T> e) => throw new NotImplementedException(); public static void Reverse<T>(this Span<T> s) => throw new NotImplementedException(); }

Runtime exception in expression lambdas when compiled with interpretation:

using System.Linq.Expressions; // ok with C# 13, but causes a runtime exception in C# 14 // because it uses overload with ReadOnlySpan<T> M((array, num) => array.Contains(num)); void M(Expression<Func<int[], int, bool>> e) => e.Compile(preferInterpretation: true);

Learn more in the official Microsoft documentation:

19 August 2025