Description
Immutable arrays are unable to be passed to methods accepting read-only spans.
Reproduction Steps
public static void Main(string[] args)
{
// Have some ImmutableArray<T> (in this case T being byte)
var array = "Gem was here"u8.ToImmutableArray();
// Some method accepting ReadOnlySpan<T> (again, in this case, byte)
SomeMethod(array);
}
public static void SomeMethod(ReadOnlySpan<byte> span)
{
// Lets keep this quite simple
Console.WriteLine(Encoding.UTF8.GetString(span));
}
Expected behavior
Actual behavior
CS1503: Argument 1: cannot convert from System.Collections.Immutable.ImmutableArray<byte> to System.ReadOnlySpan<byte>
Regression?
None that I know of
Known Workarounds
- Adding
.AsSpan() on the ImmutableArray<T>, which gives a ReadOnlySpan<T>
- Using
T[] instead of ImmutableArray<T>, which can be passed fine to methods accepting ReadOnlySpan<T>
Configuration
.NET 10, Windows 10, x64
Other information
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-14.0/first-class-span-types
Description
Immutable arrays are unable to be passed to methods accepting read-only spans.
Reproduction Steps
Expected behavior
Actual behavior
CS1503: Argument 1: cannot convert from
System.Collections.Immutable.ImmutableArray<byte>toSystem.ReadOnlySpan<byte>Regression?
None that I know of
Known Workarounds
.AsSpan()on theImmutableArray<T>, which gives aReadOnlySpan<T>T[]instead ofImmutableArray<T>, which can be passed fine to methods acceptingReadOnlySpan<T>Configuration
.NET 10, Windows 10, x64
Other information
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-14.0/first-class-span-types