Do I have to hash twice in C#?
- by Joe H
I have code like the following:
class MyClass
{
string Name;
int NewInfo;
}
List<MyClass> newInfo = .... // initialize list with some values
Dictionary<string, int> myDict = .... // initialize dictionary with some values
foreach(var item in newInfo)
{
if(myDict.ContainsKey(item.Name)) // 'A' I hash the first time here
myDict[item.Name] += item.NewInfo // 'B' I hash the second (and third?) time here
else
myDict.Add(item.Name, item.NewInfo);
}
Is there any way to avoid doing two lookups in the Dictionary -- the first time to see if contains an entry, and the second time to update the value? There may even be two hash lookups on line 'B' -- one to get the int value and another to update it.