Do I have to hash twice in C#?
Posted
by Joe H
on Stack Overflow
See other posts from Stack Overflow
or by Joe H
Published on 2009-04-24T18:42:55Z
Indexed on
2010/05/21
8:50 UTC
Read the original article
Hit count: 174
c#
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.
© Stack Overflow or respective owner