Is it possible to create a list that can be access by either an index or a key?
I am looking for a Collection type that already exists but has this facility, I want to avoid redefining the indexers
There's a similar question at What is the best data structure in .NET for look-up by string key or numeric index?.
Have a look at KeyedCollection:
class IndexableDictionary<TKey, TItem> : KeyedCollection<TKey, TItem>
{ Dictionary<TItem, TKey> keys = new Dictionary<TItem, TKey>();
protected override TKey GetKeyForItem(TItem item) { return keys[item];}
public void Add(TKey key, TItem item)
{ keys[item] = key;
this.Add(item);
}
}
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