[API Proposal]: allow collection expressions for 'ReadOnlyCollection<T>' #110161
Labels
api-ready-for-review
API is ready for review, it is NOT ready for implementation
area-System.Collections
Milestone
Background and motivation
This proposal is about allowing collection expressions to be used to initialize
ReadOnlyCollection<T>
instances.There are two main motivations for this:
ReadOnlyCollection<T>
value from some array expression. Being able to use a collection expression would make such scenarios nicer and less verbose.IEnumerable<int> e = [1, 2, 3]
) results in code that doesn't work at all with AOT. To work around this,ReadOnlyCollection<T>
is a very easy and convenient choice: you can simply replace readonly interfaces for public properties in viewmodels with this, and that will make the code perfectly AOT compatible (because CsWinRT will be able to "see" the concrete type being used). Currently though, you cannot use collection expressions with it, meaning when you change properties to use this type, you'll be forced to switch to more verbose syntax with an object creation expression and a nested collection expression. It would be much nicer to just be able to use a collection expression directly.I'd be happy to contribute this if it gets approved 🙂
Note
Extracted from #108457 (comment)
API Proposal
Note
The contract of this builder method should make it clear that the returning type is guaranteed to be exactly
ReadOnlyCollection<T>
.API Usage
Risks
No risk. This enables new syntax that couldn't be used before, and developers using that would not care about the concrete type of the inner list being used. Furthermore,
ReadOnlyCollection<T>
doesn't expose the inner list anyway, so it should not even matter at all in any case.The text was updated successfully, but these errors were encountered: