Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Dictionary" with key as part of stored elements?

Tags:

c#

dictionary

Seems like I forgot a typename ... Something inside my head keeps telling me that I have stumbled across a Dictionary that basicly uses a user-defined Comparer for lookups. But I somehow can't find that class in the depths of .Net anymore.

As I have no real idea how to describe what I am looking for without describing a possible implementation, I will also give an example of what I wan't to do.

I am basicly looking for a way to achieve the following (pseudocode):

class CustomId
{
   // [...]
}
class Element
{
   CustomId id;
}
Container<CustomId, Element> myContainer = new Container(myCustomComparer)
myContainer.Add(new Element()) // No key specified
myElement = myContainer[new CustomId(...)]; // Retrieval with custom id

Maybe this technically isn't quite a dictionary, but I hope the idea is clear. Basicly the key is part of the stored element.

like image 403
Marcus Riemer Avatar asked Dec 10 '22 13:12

Marcus Riemer


1 Answers

I think you are looking for KeyedCollection<TKey, TItem>. Create a class that inherits from this class and override the GetKeyForItem() method.

like image 183
Bryan Avatar answered Dec 26 '22 03:12

Bryan