-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
FYI about: dotnet/roslyn#71373
Offer use-collection-expr when targeting interfaces by CyrusNajmabadi · Pull Request #71373 · dotnet/roslyn
Fixes #70996.
This will let the user know that semantics may change. There is an option to globally disable this and never offer 'use collection expr' in these cases.
It's going to tweak IDE0300-IDE0305. Specifically, all of those existing analyzers/fixers will now also be offered when the type being targetted is one of the 5 well known interface types the langauge allows for collection expressions. Specifically:
IEnumerable<T>, ICollection<T>, IList<T>, IReadOnlyCollection<T> and IReadOnlyList<T>.
However, this could end up changing semantics from what theoriginal code was. For example: if you had IEnumerable<int> x = new int[] { 1, 2, 3 };, then in the original code, an array is produced, but in the new code IE<int> x = [1, 2, 3]; an internal compiler synthesized type is produced instead. This could be observable if the user did an is check or did a cast.
Because of that, this behavior is controllable with an option:
The existing option dotnet_style_prefer_collection_expression previously allowed the values false and true. It now has the values never, when_types_exactly_match, and when_types_loosely_match.
false and never are the same thing (for back compat)
true and when_types_exactly_match are the same thing (again, for back compat).
The new option is when_types_loosely_match. Which allows for this new behavior. This value is also the default if nothing has been specified in the editor config. If a user does not like this default, they can use never or when_types_exactly_match to change things.