dotnet / dotnet/runtime

`ImmutableArray<T>` cannot be passed to methods accepting `ReadOnlySpan<T>`

Open
#126,758 11 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-System.Collections help wanted
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

Immutable arrays are unable to be passed to methods accepting read-only spans.

### Reproduction Steps

```cs
public static void Main(string[] args)
{
// Have some ImmutableArray (in this case T being byte)
var array = "Gem was here"u8.ToImmutableArray();

// Some method accepting ReadOnlySpan (again, in this case, byte)
SomeMethod(array);
}

public static void SomeMethod(ReadOnlySpan 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` to `System.ReadOnlySpan`

### Regression?

None that I know of

### Known Workarounds

- Adding `.AsSpan()` on the `ImmutableArray`, which gives a `ReadOnlySpan`
- Using `T[]` instead of `ImmutableArray`, which can be passed fine to methods accepting `ReadOnlySpan`

### 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.