Generic Key/Value pair collection in that preserves insertion order?

前端 未结 9 1282
刺人心
刺人心 2020-12-09 01:40

I\'m looking for something like a Dictionary however with a guarantee that it preserves insertion order. Since Dictionary is a hashtable, I do not think it does.<

9条回答
  •  忘掉有多难
    2020-12-09 02:19

    If you need constant complexity of Add, Remove, ContainsKey and order preservation, then there's no such generic in .NET Framework 4.5.

    If you're okay with 3rd party code, take a look at my repository (permissive MIT license): https://github.com/OndrejPetrzilka/Rock.Collections

    There's OrderedDictionary collection:

    • source code based on classic Dictionary (from .NET Core)
    • preserves order of insertions and allows manual reordering
    • features reversed enumeration
    • has same operation complexities as Dictionary
    • Add and Remove operations are ~20% slower compared to Dictionary
    • consumes 8 more bytes of memory per item

提交回复
热议问题