Dictionary<string, List<string>> myDictionary = new Dictionary<string, List<string>>();
myDictionary[key].Add(new string(value));
List<string> valueList;
if (myDictionary.TryGetValue(key, out valueList))
{
for each (string value in valueList)
{
...
}
}
How to add and retrieve the data with key value pair without using dictionary?
List<KeyValue> myList; // assume KeyValue has members key and value
....
myList.Sort((kv1, kv2) => kv1.key.CompareTo(f2.key));
int start = 0;
int end = myList.Count;
int result = 0;
while (start < end)
{
int mid = (start + end) / 2;
result = key.CompareTo(myList[mid].key));
if (result <= 0)
{
end = mid;
}
else
{
start = mid + 1;
}
}
if (result == 0)
{
// first key-value pair found at index start
// check next keys from start + 1 whether there are more key-value pairs containing the same key
}
You can however replicate this with a 2 dimensional array, or List of POCO objects. Doing that is like reinventing the wheel though.