Question

I'm trying to wrap my head around why the IDictioary<TKey, TValue>.Values property is of type ICollection<TValue> as opposed to IEnumerable<TValue>. The .NET implementation of the Values property implements the interface property explicitly and then exposes the Values property as type Dictionary<TKey, TValue>.ValueCollection which throws an exception when you try to add or remove values from it.

This seems like a complete hack. Because of the nature of a dictionary, does it ever make sense to create an implementation which allows you get get a collection of the values in the dictionary and add / remove values from it?

Was it helpful?

Solution

This allows you to use the Count property and the Contains() method on a dictionary interface.

You're right; it is an ugly hack.
The ideal soltuion would have been to use the new (to .Net 4.5) IReadOnlyCollection<T> interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top