-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add CollectionMarshal.AsSpan<T>(ObservableCollection<T>) overload #110024
Comments
Tagging subscribers to this area: @dotnet/area-system-collections |
This is notably an implementation detail. Exposing such a 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. |
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 because List has that return type. |
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 |
Can you use |
It's possible, although this risks breaking in the future if the private field is changed. |
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.
The text was updated successfully, but these errors were encountered: