If you need to keep all of the values, but still be able to retrieve the most recent one for a given key, you could use a
List
or
LinkedList
to store all the values and a
Dictionary
to save the values for a given key, something like this:
public class DataStructure<br />{<br /> public void Add(int key,MyObject data)<br /> {<br /> DataList.AddTail(new Entry(key,data));<br /> DataDictionary[key] = data;<br /> }<br /> public MyObject Find(key)<br /> {<br /> MyObject data = null;<br /> DataDictionary.TryGetValue(key,out data);<br /> return data;<br /> }<br /> private class Entry<br /> {<br /> public Entry(int key,MyObject data)<br /> {<br /> Key = key;<br /> Data = data;<br /> }<br /> public int Key;<br /> public MyObject Data;<br /> }<br /> private LinkedList<Entry> DataList = new LinkedList<Entry>();<br /> private Dictionary<int,MyObject> DataDictionary = new Dictionary<int,MyObject>();<br />}
Note that I've omitted some things here, like the ability to enumerate the list.