Proposal: Opt-out attribute for collection initialization #8811
Unanswered
louthy
asked this question in
Language Ideas
Replies: 2 comments
-
Note. This needs no compiler/Roslyn work. Everything above can be handled today already using the analyzer subsystem. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Conversely, such a feature could be used to enable collection initializers for arbitrary types, as suggested a few times. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Collection initializers look for the
Add
method on a type so that they can repeatedly callAdd
to initialise a collection. This is problematic for immutable collections that have a default constructor (or are structs).Example:
The following will result in an empty collection:
My proposal is to allow an attribute called
[NoCollectionInitialization]
to be applied to theAdd
method and for the compiler to then ignore it when resolving:This proposal has no backward compatibility issues and, one would assume, be the lowest impact in terms of code-change.
Beta Was this translation helpful? Give feedback.
All reactions