System.Linq.Lookup vs. Wintellect.PowerCollections.MultiDictionary
I still use Wintellect's PowerCollections library, even though it is aging and not maintained because it did a good job covering holes left in the standard MS Collections libraries. But LINQ and C# 4.0 are poised 开发者_JAVA技巧to replace PowerCollections...
I was very happy to discover System.Linq.Lookup because it should replace Wintellect.PowerCollections.MultiDictionary
in my toolkit. But Lookup
seems to be immutable! Is that true, can you only created a populated Lookup by calling ToLookup
?
Yes, you can only create a Lookup
by calling ToLookup
. The immutable nature of it means that it's easy to share across threads etc, of course.
If you want a mutable version, you could always use the Edulinq implementation as a starting point. It's internally mutable, but externally immutable - and I wouldn't be surprised if the Microsoft implementation worked in a similar way.
Personally I'm rarely in a situation where I want to mutate the lookup - I would prefer to perform appropriate transformations on the input first. I would encourage you to think in this way too - I find myself wishing for better immutability support from other collections (e.g. Dictionary
) more often than I wish that Lookup
were mutable :)
That is correct. Lookup
is immutable, you can create an instance by using the Linq ToLookup()
extension method. Technically even that fact is an implementation detail since the method returns an ILookup
interface which in the future might be implemented by some other concrete class.
精彩评论