当我遍历一个Dictionary(.NET通用数据结构)时,它是否按照我添加的顺序? [英] When I iterate through a Dictionary (.NET generic data structure) will it be in the same order that I added them?

查看:111
本文介绍了当我遍历一个Dictionary(.NET通用数据结构)时,它是否按照我添加的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通常使用密钥访问的字典,所以我需要快速的随机访问读取。然而,对于一个函数,我需要处理字典中顺序重要的每个项目。在测试中似乎工作正常。取决于词典中的项目顺序是否可行?

解决方案

否。如果您需要保留订单,您还应该有一个项目列表。您可以将您需要的所有操作封装在您自己的集合类中,同时更新字典和列表。



不幸的是,有一个支持这个本身的字典 - 这是一个相当常见的请求--Java做的,如 LinkedHashMap


I have a dictionary that I normally access with a key, so I need fast random access reads. However for one function I need to process every item in the dictionary where the order is important. It seems to be working OK in tests. Is it OK to depend on the order of items in a dictionary?

解决方案

No. If you need to keep an order, you should have a list of items as well. You could encapsulate all the operations you need in your own collection class, which would update both the dictionary and the list at the same time.

It's unfortunate that .NET doesn't have a dictionary which supports this itself - it's a reasonably common request - Java does, as LinkedHashMap.

这篇关于当我遍历一个Dictionary(.NET通用数据结构)时,它是否按照我添加的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆