C# Dictionary<> and mutable keys

后端 未结 5 1223
盖世英雄少女心
盖世英雄少女心 2020-12-06 18:09

I was told that one of the many reasons strings were made immutable in the C# spec was to avoid the issue of HashTables having keys changed when references to the string key

5条回答
  •  青春惊慌失措
    2020-12-06 18:51

    The Dictionary<> class does nothing to protect itself against a mutable key object being changed. It's up to you to know whether or not the class you're using as a key is mutable, and to avoid it if possible.

提交回复
热议问题