更改 C# 字典中项目的基数
我有一本字典,就像
Dictionary<Foo,String> fooDict
我逐步浏览字典中的所有内容一样,例如,
foreach (Foo foo in fooDict.Keys)
MessageBox.show(fooDict[foo]);
它按照将 foo 添加到字典中的顺序执行此操作,因此添加的第一个项目是返回的第一个 foo 。
如何更改基数,例如,添加的第三个 foo 将是返回的第二个 foo? 换句话说,我想改变它的“索引”。
I've got a dictionary, something like
Dictionary<Foo,String> fooDict
I step through everything in the dictionary, e.g.
foreach (Foo foo in fooDict.Keys)
MessageBox.show(fooDict[foo]);
It does that in the order the foos were added to the dictionary, so the first item added is the first foo returned.
How can I change the cardinality so that, for example, the third foo added will be the second foo returned? In other words, I want to change its "index."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您阅读 MSDN 上的文档,您会看到以下内容:
“返回项目的顺序未定义。”
您无法保证顺序,因为字典不是列表或数组。 它意味着通过键查找值,任何迭代值的能力只是一种方便,但顺序不是您应该依赖的行为。
If you read the documentation on MSDN you'll see this:
"The order in which the items are returned is undefined."
You can't gaurantee the order, because a Dictionary is not a list or an array. It's meant to look up a value by the key, and any ability to iterate values is just a convenience but the order is not behavior you should depend on.
您可能对
OrderedDicationary
类来自
System .Collections.Specialized
命名空间。如果你看看最底部的评论,就会发现来自 MSFT 的人发布了这个有趣的注释:
我认为从此类派生并制作 OrderedDictionary 的通用版本将是微不足道的。
You may be interested in the
OrderedDicationary
class that comes inSystem.Collections.Specialized
namespace.If you look at the comments at the very bottom, someone from MSFT has posted this interesting note:
I think it would be trivial to derive from this class and make a generic version of OrderedDictionary.
我没有在该领域接受过充分的教育来正确回答这个问题,但我感觉字典根据键对值进行排序,以便执行快速键搜索。 这表明字典是根据键比较按键值排序的。 但是,查看 object 方法,我假设它们使用哈希码来比较不同的对象,因为对键使用的类型没有要求。 这只是一个猜测。 更有知识的人应该填写更多细节。
当字典的目的是使用任意类型进行索引时,为什么您对操作字典的“索引”感兴趣?
I am not fully educated in the domain to properly answer the question, but I have a feeling that the dictionary sorts the values according to the key, in order to perform quick key search. This would suggest that the dictionary is sorted by key values according to key comparison. However, looking at object methods, I assume they are using hash codes to compare different objects considering there is no requirement on the type used for keys. This is only a guess. Someone more knowledgey should fill in with more detail.
Why are you interested in manipulating the "index" of a dictionary when its purpose is to index with arbitrary types?
我不知道是否有人会觉得这有用,但这就是我最终弄清楚的。 它似乎有效(我的意思是它不会抛出任何异常),但距离测试它是否如我希望的那样有效还有很长的路要走。 不过,我以前也做过类似的事情。
I don't know if anyone will find this useful, but here's what I ended up figuring out. It seems to work (by which I mean it doesn't throw any exceptions), but I'm still a ways away from being able to test that it works as I hope it does. I have done a similar thing before, though.
简而言之,因为字典“代表键和值的集合”,所以不应该有任何方法。 这并不意味着任何排序。 您可能发现的任何 hack 都超出了类的定义,并且可能会发生更改。
您可能应该首先问自己在这种情况下是否真的需要字典,或者您是否可以使用键值对列表。
否则,这样的东西可能会有用:
客户端代码看起来像这样:
更新:出于某种原因,它不允许我评论我自己的答案。
无论如何,IndexableDictionary 与 OrderedDictionary 的不同之处在于
The short answer is that there shouldn't be a way since a Dictionary "Represents a collection of keys and values." which does not imply any sort of ordering. Any hack you might find is outside the definition of the class and may be liable to change.
You should probably first ask yourself if a Dictionary is really called for in this situation, or if you can get away with using a List of KeyValuePairs.
Otherwise, something like this might be useful:
With client code looking something like this:
UPDATE: For some reason it won't let me comment on my own answer.
Anyways, IndexableDictionary is different from OrderedDictionary in that