skip to content

In C# 14, what do the new implicit span conversions let you write with arrays, strings and ReadOnlySpan<T> that C# 13 rejected?

level: middleimportance: should knowfreq 35%

answer

  1. conversions built into the language
  2. extension receivers
  3. generic type inference
  4. ReadOnlySpan covariance
  5. no allocation added

basics

~20 s

C# 14 makes array-to-span, span-to-ReadOnlySpan and string-to-ReadOnlySpan<char> built-in implicit conversions. Arrays and strings can now call extension methods declared on spans, generic methods taking ReadOnlySpan<T> infer T from an array, and ReadOnlySpan<string> converts to ReadOnlySpan<object>.

solid answer

~40 s

Before C# 14 the span conversions were only user-defined `implicit operator`s, so passing an array to a `ReadOnlySpan<T>` parameter worked, but little else did. C# 14 adds an **implicit span conversion** to the language: `T[]` to `Span<T>` and `ReadOnlySpan<T>`, `Span<T>` to `ReadOnlySpan<T>`, `string` to `ReadOnlySpan<char>`, plus covariance from `ReadOnlySpan<string>` to `ReadOnlySpan<object>`. That makes three things compile that C# 13 rejected: `codes.StartsWith(200)` on an `int[]` (an extension on `ReadOnlySpan<T>`; CS1929 before), `Max(codes)` for `Max<T>(ReadOnlySpan<T>)` (CS0411 before), and the covariant assignment (CS0029 before). The conversion still just wraps the existing memory, so nothing extra is allocated. It can also change which overload a call binds to.

code

csharp · 32 lines
csharp
int[] codes = [200, 200, 404, 500];
string[] names = ["api", "web"];

// 1. A MemoryExtensions method declared on ReadOnlySpan<T>, called on an array.
Console.WriteLine(codes.StartsWith(200));            // True

// 2. Generic inference through the conversion.
Console.WriteLine(Stats.Max(codes));                  // 500

// 3. Covariance: ReadOnlySpan<string> -> ReadOnlySpan<object>.
ReadOnlySpan<string> hosts = names;
ReadOnlySpan<object> items = hosts;
Console.WriteLine(items.Length);                      // 2

// 4. Your own extension on ReadOnlySpan<char>, called on a string.
Console.WriteLine("GET /health".IsHealthProbe());     // True

static class Stats
{
    public static T Max<T>(ReadOnlySpan<T> values) where T : IComparable<T>
    {
        T best = values[0];
        foreach (T v in values) if (v.CompareTo(best) > 0) best = v;
        return best;
    }
}

static class LogExt
{
    public static bool IsHealthProbe(this ReadOnlySpan<char> line) =>
        line.EndsWith("/health", StringComparison.Ordinal);
}

go deeper

for a junior

Know that from C# 14 an array or a string can call methods written for ReadOnlySpan<T> directly, without AsSpan.

for a middle

Explain the three newly legal cases, extension receivers, generic inference and ReadOnlySpan covariance, and why user-defined operators could not do them.

for a senior

Expect overload binding to shift when a project moves to C# 14, review span overloads in your own libraries, and know the conversions add no allocation.

for a principal

Use first-class spans to shrink library surface: declare helpers once on ReadOnlySpan<T> instead of maintaining parallel array and span overloads.

## What changed in C# 14 `Span<T>` and `ReadOnlySpan<T>` have had conversion operators since they shipped: `T[]` converts to either span, and `Span<T>` converts to `ReadOnlySpan<T>`. But those were **user-defined** conversions, and the C# rules treat user-defined conversions as second-class. They are not considered for an extension method's receiver, they do not help generic type inference, and they cannot express covariance. C# 14 (shipped with .NET 10) introduces **first-class span types**: the language itself now knows an *implicit span conversion*. Per the feature specification, it exists: - from a single-dimensional array `T[]` to `Span<T>`, - from `T[]` to `ReadOnlySpan<U>` when `T` is covariance-convertible to `U`, - from `Span<T>` to `ReadOnlySpan<U>`, and from `ReadOnlySpan<T>` to `ReadOnlySpan<U>`, under the same covariance rule, - from `string` to `ReadOnlySpan<char>`. The feature is tied to `LangVersion` 14 or later; with `LangVersion` 13 the old rules apply. ## Three things that now compile | Code | C# 13 on the .NET 10 SDK | C# 14 | |---|---|---| | `int[] codes; codes.StartsWith(200)`, where `MemoryExtensions.StartsWith<T>(this ReadOnlySpan<T>, T)` | CS1929: `int[]` does not contain a definition for `StartsWith` ... requires a receiver of type `ReadOnlySpan<int>` | Binds to the span extension | | `Max(codes)` for `static T Max<T>(ReadOnlySpan<T> values)` | CS0411: the type arguments cannot be inferred from the usage | Infers `T` as `int` | | `ReadOnlySpan<object> items = hosts;` where `hosts` is a `ReadOnlySpan<string>` | CS0029: cannot implicitly convert | Compiles | | `"GET /health".IsHealthProbe()` for an extension on `ReadOnlySpan<char>` | CS1929 on the `string` receiver | Binds to the span extension | All four errors in the C# 13 column come from compiling the example below with `#:property LangVersion=13` on the .NET 10 SDK. ## Why this matters for library code The motivation in the specification is API surface. Without first-class spans, a library that wanted `array.Foo()`, `span.Foo()` and `text.Foo()` to work had to declare an overload per receiver type. With C# 14, **one extension method on `ReadOnlySpan<T>`** reaches arrays, `Span<T>` and, for `char`, strings. Teams writing parsing or numeric helpers can declare them once on `ReadOnlySpan<T>` and stop duplicating them for arrays. ## What it costs at run time Nothing new. The compiler lowers the conversions to the same helpers as before: the `op_Implicit` operators on `Span<T>` and `ReadOnlySpan<T>`, `MemoryExtensions.AsSpan(string)` for strings, and `ReadOnlySpan<T>.CastUp` for covariance. Each builds a view over existing memory. Calling `codes.StartsWith(3)` 1,000 times on an array allocated zero bytes on the SDK. ## Upgrading a codebase to C# 14 Moving a project to `LangVersion` 14, which is the default when it targets .NET 10, is mostly invisible for span code, but a few steps keep it predictable: 1. **Delete helper duplicates.** Extension methods that existed only to make an array or a string reach a span helper, such as an `AsSpan()`-and-forward wrapper, can go. 2. **Re-run the tests that pass arrays to overloaded APIs.** Calls that took an array or `IEnumerable<T>` overload can now pick a span overload; usually that is faster and equivalent, but behaviour can differ when the span overload works in place. 3. **Watch libraries that multi-target.** A library compiled with C# 14 for an older target framework may lack the newer framework's array overloads that keep binding stable; the breaking-change notes describe `Enumerable.Reverse` on arrays as the classic example, which .NET 10 itself avoids with an array overload. 4. **Prefer `ReadOnlySpan<T>` parameters in new APIs.** With covariance and receiver conversions, they now accept arrays, spans and strings with no extra code at the call site. ## Limits and edges 1. **Covariance only into `ReadOnlySpan`.** There is no `T[]` to `Span<object>` covariance, because writing through it could put the wrong type into an array. Even an explicit `object[]` that really holds a `string[]` throws `ArrayTypeMismatchException` when wrapped in `Span<object>`; array covariance itself belongs with variance rules. 2. **Not for method group conversions.** The specification keeps span conversions out of extension receivers when a method group is converted to a delegate, so `Action<int> a = array.M;` does not start binding to a span extension. 3. **Overload resolution can shift.** Because the conversion is now standard, a call with an array argument may bind to a span overload where it bound to an `IEnumerable<T>` or array overload before, and some calls become ambiguous. Those rules are covered with overload resolution; the Roslyn breaking-change notes for .NET 10 list the known cases. 4. **Patterns do not use it.** A `ref struct` in a pattern still allows only identity conversions. ## Interview angle A crisp answer says the conversions moved from library operators into the language, names the receiver, inference and covariance cases with the C# 13 errors, and adds that the runtime cost is unchanged while overload binding can change on upgrade.

  • Passing an int[] to a method with a ReadOnlySpan<int> parameter already worked in C# 12; what exactly did C# 14 add?
    Plain argument passing worked through the user-defined `implicit operator ReadOnlySpan<T>(T[])`. What did not work was everything user-defined conversions are excluded from: using the array as an extension method receiver, inferring a generic `T` from the array, and covariance between span types. C# 14 makes the conversion part of the language, so those cases compile.
  • Why does C# 14 allow ReadOnlySpan<string> to ReadOnlySpan<object> but not Span<string> to Span<object>?
    A read-only view can only read, and every string is an object, so reading through `ReadOnlySpan<object>` is safe. A writable `Span<object>` would let code store a non-string into memory that holds strings. The runtime guards array writes by throwing `ArrayTypeMismatchException`, and span creation checks the same thing, so the language offers covariance only for the read-only type.

  • Thinks arrays could never be passed to ReadOnlySpan<T> parameters before C# 14.
  • Believes the new conversions copy the array into a fresh buffer.
  • Expects C# 14 to convert a string[] implicitly to Span<object>.
  • Assumes upgrading to C# 14 can never change which overload binds.
  • Says the feature requires calling a new AsSpan overload explicitly.

Assumes C# 14 (LangVersion 14, default on .NET 10); with LangVersion 13 the three cases fail with CS1929, CS0411 and CS0029.