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:
Compilation error because of changed overload resolution:
Runtime exception in expression lambdas when compiled with interpretation:
Learn more in the official Microsoft documentation: