Dictionary Keys.Contains vs. ContainsKey: are they functionally equivalent?

前端 未结 2 1362
心在旅途
心在旅途 2020-12-06 16:20

I am curious to know if these two are functionally equivalent in all cases.

Is it possible that by changing the dictionary\'s default comparator that these two would

2条回答
  •  伪装坚强ぢ
    2020-12-06 16:34

    Although they are pretty much equivalent for Dictionary<,>, I find it's much safer to stick with ContainsKey().

    The reason is that in the future you may decide to use ConcurrentDictionary<,> (to make your code thread-safe), and in that implementation, ContainsKey is significantly faster (since accessing the Keys property does a whole bunch of locking and creates a new collection).

提交回复
热议问题