Insert into a generic dictionary with possibility of duplicate keys?
Posted
by Chris Clark
on Stack Overflow
See other posts from Stack Overflow
or by Chris Clark
Published on 2010-04-02T15:33:29Z
Indexed on
2010/04/02
15:43 UTC
Read the original article
Hit count: 261
Is there any reason to favor one of these approaches over the other when inserting into a generic dictionary with the possibility of a key conflict? I'm building an in-memory version of a static collection so in the case of a conflict it doesn't matter whether the old or new value is used.
If Not mySettings.ContainsKey(key) Then
mySettings.Add(key, Value)
End If
Versus
mySettings(key) = Value
And then of course there is this, which is obviously not the right approach:
Try
mySettings.Add(key, Value)
Catch
End Try
Clearly the big difference here is that the first and second approaches actually do different things, but in my case it doesn't matter. It seems that the second approach is cleaner, but I'm curious if any of you .net gurus have any deeper insight. Thanks!
© Stack Overflow or respective owner