Skip to content

ImmutableArray<T> cannot be passed to methods accepting ReadOnlySpan<T> #126758

@rreminy

Description

@rreminy

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

Gem was here

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions