Skip to content

Conversation

@stephentoub
Copy link
Member

e.g. by default this will trigger IDE0300:

IEnumerable<int> source = new int[] { 0, 1, 2 };

but if that's changed to:

IEnumerable<int> source = [0, 1, 2];

the compiler tries to protect the readonly-ness of the enumerable from upcasting and emits something along the lines of:

IEnumerable<int> source = new ReadOnlyArray<int>(new int[] { 0, 1, 2 });

which is then both an extra allocation and hinders some downstream optimizations from code such as we have in LINQ that tests whether the source is a T[].

Putting this into the .editorconfig prevents the IDE from recommending switching to collection expressions in such cases.

@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 25, 2024
@stephentoub stephentoub requested a review from buyaa-n March 25, 2024 19:53
@stephentoub stephentoub added area-Infrastructure and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 25, 2024
Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation in the description.

@stephentoub stephentoub merged commit 25f3883 into dotnet:main Mar 25, 2024
@stephentoub stephentoub deleted the collectionexpressions branch March 25, 2024 22:14
@github-actions github-actions bot locked and limited conversation to collaborators Apr 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants