Open zgabi opened 1 day ago
Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.
which is always a List
This is notably an implementation detail. Exposing such a CollectionsMarshal
API requires us agreeing that the implementation will never change from some linear/contiguous buffer.
That isn't necessarily desirable for all collection types and needs to be considered on a per type basis.
Why would you want a span for the collection? The purpose of that type is to notify about changes, and a span would bypass that logic.
And no events will be raised (Replace or Update https://github.com/dotnet/runtime/issues/79713) etc.
I think this is an important concern, arguably it's violating the contract of the collection.
Actually I only need ReadOnlySpan, not a writeable. I wrote Span
So the motivation is iteration performance? I don't think this would justify a CollectionsMarshal-like method or us pinning the implementation detail of ObservableCollection.
Motivation is avoid extra allocation. I'd like to pass the span to a method which is also called from another part of the code where the source is not an IList/ICollection. (For example stackallocked "list")
I don't think this is sufficient to justify such an API. If you want to avoid allocations in that context you might consider calling CopyTo
on a pooled buffer.
Background and motivation
Currently it is not possible (without Reflection or other "hack") to access the internal items of the
ObservableCollection<T>
, which is always aList<T>
as aSpan<T>
It would be nice to allow to access the internal items as a
Span<T>
API Proposal
API Usage
Alternative Designs
Add a method or property to the ObservableCollection to get the internal list. But I think the CollectionMarshal solution would be better for consistency.
Risks
Same as for CollectionMarshal.AsSpan(List), so warn the users that no items should be added or removed from the collection while the span is used. And no events will be raised (Replace or Update #79713) etc.