简体   繁体   中英

Generic collection with IEqualityComparer in constructor

Is there any collection in framework like that?

public class DesiredCollection<T>
{
    public DesiredCollection(IEqualityComparer<T> comparer) 
    {
    }
}

Requirements:

  • generic collection;
  • IEqualityComparer or lambda-expression to determine equality;
  • no need to define key type;
  • getting item by key;
  • getting all items as IEnumerable.

Various collections take IEqualityComparer<T> in their constructors (usually as one overload out of several). In particular, Dictionary<TKey, TValue> and HashSet<T> fall into this category. They use the equality comparer to check for the existence of keys / elements.

However, you haven't really expressed anything other than the need for a constructor taking an equality comparer. Presumably you'll need more than that... what are you trying to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM