Yes there is - System.Collections.ObjectModel.KeyedCollection<TKey, TValue> .
This essay, and I do not see specific derived classes in the structure, but all you need to implement is GetKeyForItem , as far as I can see. For example, you can do this with a delegate:
public class DelegatingKeyedCollection<TKey, TItem> : System.Collections.ObjectModel.KeyedCollection<TKey, TItem> { private readonly Func<TItem, TKey> keySelector; public DelegatingKeyedCollection(Func<TItem, TKey> keySelector) { this.keySelector = keySelector; } protected override TKey GetKeyForItem(TItem item) { return keySelector(item); } }
Jon skeet
source share