Change object-or-collection-initializer to object-and-collection-initializer #597
Unanswered
mariusGundersen
asked this question in
General
Replies: 2 comments
-
For var dictionary = new Dictionary<string, string>(someOtherDictionary)
{
["Alpha"] = "one",
["Beta"] = "two"
}; Is this also "not as nice"? |
Beta Was this translation helpful? Give feedback.
0 replies
-
That's my point, you can't combine that with a property, which is what I'm asking for. |
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.
-
According to this SO answer it's not possible to combine object initializer with collection initializer. I have a situation where this would be useful.
First, lets have a look at just the collection initializer. It uses some simple pattern matching to find an
Add()
method with the correct signature, and is even able to work with extension methods. This lets me create this very useful extension method:Using this it's now possible to create a new
ICollection
and copy the contents of anotherIEnumerable
:With a dictionary I would like to use the index initializer:
Unfortunately this is not possible, since the
["Alpha"] = "one"
is using the object initializer. It is possible to do this using the existing collection initializer:But this is not as nice.
Beta Was this translation helpful? Give feedback.
All reactions