As the title says, why does the Dictionary collection in C# contain a .Distinct() extension as if it's possible for a Dictionary to contain non-distinct keys? Is there legitimate reasoning behind this or am I reading too far into it?
Dictionary<TKey, TValue> implements IEnumerable<KeyValuePair<TKey, TValue>> which has a Distinct extension. The Dictionary class itself doesn't have an implementation of Distinct
Calling the Distinct is translated to a call to the static extension method:
Enueramble.Distinct(IEnumerable<T> source)
Which is unneccesary for Dictionary since keys are distinct (and hence key/value pairs are distinct) but technically there's nothing wrong with it.
The Distinct applies to the IEnumerable<KeyValuePair<TKey, TValue>> interface from a Dictionary<TKey, TValue>. While it doesn't make sense because a dictionary have unique keys, the extension will be present solely because Dictionary<TKey, TValue> implements IEnumerable<KeyValuePair<TKey, TValue>>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With