[Mono-dev] Bug in System.Core Lookup<TKey,TElement>

Bassam Tabbara bassam at symform.com
Tue Dec 23 23:58:27 EST 2008

I'm running Mono 2.0-1 on Linux. The implementation of Lookup extension method on IEnumerable does not seem to handle a case insensitive comparer correctly. The code does the following:

public static Lookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
                       Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
                       if (source == null || keySelector == null || elementSelector == null)
                               throw new ArgumentNullException ();

                       Dictionary<TKey, List<TElement>> dictionary = new Dictionary<TKey, List<TElement>> (comparer ?? EqualityComparer<TKey>.Default);
                       foreach (TSource element in source) {
                               TKey key = keySelector (element);
                               if (key == null)
                                      throw new ArgumentNullException ();
                               if (!dictionary.ContainsKey (key))
                                      dictionary.Add (key, new List<TElement> ());
                               dictionary [key].Add (elementSelector (element));
                       return new Lookup<TKey, TElement> (dictionary);

The last line of the this method does not pass the comparer to the Lookup<TKey,TElement> class and hence all comparisons are done with the wrong comparer.

Please let me know if you'd like me to test a patch.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20081223/144429e1/attachment.html 

More information about the Mono-devel-list mailing list