Which is faster/more efficient: Dictionary or Dictionary?

后端 未结 4 1017
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 16:27

Are enum types faster/more efficient than string types when used as dictionary keys?

IDictionary or IDict         


        
4条回答
  •  礼貌的吻别
    2021-02-05 17:00

    Certainly the enum version is better (when both are applicable and make sense, of course). Not just for performance (it can be better or worse, see Rashack's very good comment) as it's checked compile time and results in cleaner code.

    You can circumvent the comparer issue by using Dictionary and casting enum keys to ints or specifying a custom comparer.

提交回复
热议问题