-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Synthesize type for collection expressions targeting IEnumerable<T>, IReadOnlyCollection<T>, or IReadOnlyList<T> #69623
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Is it valid to use (the former does a length check, perhaps it could be added to the latter too but it would be nice if the runtime could avoid the allocation altogether.) |
List.AsReadOnly would be 3 allocations. The array, the surrounding list, and the surrounding readonly list. This would be valid, but wasteful. |
Using that helper wouldn't change the number of allocations (a List, or array is already allocated), this would only save the type being synthesized. But I suppose using a helper opens up the possibility of optimizing things further in the runtime. |
an extra allocation on every instance seems a lot worse than just having the extra synthesized type :) |
That's already the case: |
It shouldn't be. And, if that's teh case, we should fix that. The synthesized list should just hold the backing data. |
That should be easy to implement except that it would incur a copy for |
Note, that's only talking about unknown-length collection expressions; what I was getting at there was that before collection expressions, one would use the array instance directly, IReadOnlyCollection<T> M() { return new[] { 1 , 2 , 3 }; } Using collection expressions, you will need to pay extra for the wrapper type. IReadOnlyCollection<T> M() { return [1 , 2 , 3]; } The wrapper type for |
@alrz my overall point is this:
For the IReadOnlyXXX interfaces that means:
|
For collection expressions of the target types
IEnumerable<T>
,IReadOnlyCollection<T>
, orIReadOnlyList<T>
, generate an instance of a singleinternal
synthesized type.The synthesized type:
IEnumerable<T>
,IReadOnlyCollection<T>
,IReadOnlyList<T>
, andICollection<T>
andIList<T>
true
forICollection<T>.IsReadOnly
System.NotSupportedException
from mutating methods inICollection<T>
andIList<T>
, includingIList<T>.this[int] { set; }
For collection expressions of the target types
ICollection<T>
orIList<T>
, the compiler will generate instances ofList<T>
(no changes).Relates to test plan #66418
The text was updated successfully, but these errors were encountered: