如何获得字典中的第一项? [英] How to get the first item in Dictionary?

查看:669
本文介绍了如何获得字典中的第一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取添加到Dictionary对象的第一个项目。

我尝试过myDictionary [0],但它不受支持?


问候

解决方案

弗兰克,


不,它不是,因为字典没有维护插入

元素的顺序。如果你需要跟踪这个,你应该使用

System.Collections.Specialized命名空间中的OrderedDictionary类

(非泛型)或KeyedCollection类in

System.Collections.ObjectModel命名空间(通用)。

-

- Nicholas Paldino [.NET / C#MVP] < br $> b $ b - mv*@spam.guard.caspershouse.com


Frank Rizzo < no ** @no.comwrote in message

news:e4 ************** @ TK2MSFTNGP05.phx.gbl ...
< blockquote class =post_quotes>
如何获取添加到Dictionary对象的第一个项目。我已经尝试了我的字典[0],但它不受支持吗?


问候



Frank Rizzo写道:


如何获取添加到Dictionary对象的第一个项目。

我已经尝试了myDictionary [0],但它不受支持?



使用通用实现是不可能的,因为底层

存储是哈希表,并且返回项目的顺序是

未定义。你得到的是一个基于钥匙的物品,而不是基于它添加了
的顺序。

-

Tom Porterfield


2007年6月4日星期一09:07:56 -0700,Frank Rizzo< no ** @ none.comwrote:


如何获取添加到Dictionary对象的第一个项目。

我已经尝试了myDictionary [0],但它不受支持?



我不确定你能不能。由于Dictionary是哈希表,因此项目不是按照词典添加的顺序存储的。


您可以尝试使用foreach()并抓住第一个返回的项目是

,或使用Dictionary.Values [0]。但我怀疑其中两个都将返回带有最低值哈希码的项目。它可能不会是你必须是你添加的第一个(事实上,大部分时间可能都不会是
)。


如果您需要保留集合中的订单,我建议您使用

SortedDictionary<泛型类,其中您使用的密钥是一些

单调递增值(DateTime.Now,或者只是一个简单的整数

计数器,例如)。在这种情况下,集合*确实*有订单,

并且有必要请求它为第一个元素。


Pete


How do I get the first item that was added to the Dictionary object.
I''ve tried myDictionary[0], but it''s not supported?

Regards

解决方案

Frank,

No, it is not, as the dictionary doesn''t maintain an order in which the
elements are inserted. If you need to keep track of this, you should use
the OrderedDictionary class in the System.Collections.Specialized namespace
(which is not generic) or the KeyedCollection class in the
System.Collections.ObjectModel namespace (which is generic).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.comwrote in message
news:e4**************@TK2MSFTNGP05.phx.gbl...

How do I get the first item that was added to the Dictionary object. I''ve
tried myDictionary[0], but it''s not supported?

Regards



Frank Rizzo wrote:

How do I get the first item that was added to the Dictionary object.
I''ve tried myDictionary[0], but it''s not supported?

That''s not possible using the generic implementation as the underlying
storage is a hashtable and the order in which items are returned is
undefined. You get an item based on key, not based on the order that it
was added.
--
Tom Porterfield


On Mon, 04 Jun 2007 09:07:56 -0700, Frank Rizzo <no**@none.comwrote:

How do I get the first item that was added to the Dictionary object.
I''ve tried myDictionary[0], but it''s not supported?

I''m not sure you can. Since Dictionary is a hashtable, items are not
stored in order of addition to the Dictionary.

You could try using foreach() and just grabbing the first item that is
returned, or using Dictionary.Values[0]. But I suspect both of those will
just return the item with the lowest-valued hash code. It probably won''t
necessarily be the first one you added (and in fact, probably won''t be
most of the time).

If you need to preserve order in your collection, I''d recommend using the
SortedDictionary<generic class instead, where the key you use is some
monotonically increasing value (DateTime.Now, or just a simple integer
counter, for example). In that case, the collection *does* have order,
and it makes sense to ask it for the "first" element.

Pete


这篇关于如何获得字典中的第一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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