Insert into a generic dictionary with possibility of duplicate keys?
- by Chris Clark
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!