想要一个维护订购的关键价值集合 [英] Want a key value collection that maintains ordering

查看:88
本文介绍了想要一个维护订购的关键价值集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,.Net中有一个关键值集合,它维护了我添加项目的

订单。


我找到了我正在创建一些包含名称

然后是一些对象的类。我更喜欢使用一些集合,

维护我添加内容的顺序(如ArrayList),但是

将一个键映射到一个值。


我认为NameValueCollection是正确的 - 但它没有

保证订购。


如果它没有'不存在,为什么不呢?如果它不是什么是正确的建立它的方式来构建它吗?只需将您的类包装在ArrayList的实例周围,并使用一些内部类来获取Add方法中给出的键

并使用两个成员实例化此泛型类(a字符串键和

一个对象值),然后将其添加到内部ArrayList?


谢谢,

新手

Hi all, is there a key value collection in .Net that maintains the
ordering in which I add items.

I''m finding that I''m creating a number of classes that contain a name
and then some object. I would prefer just to use some collection that
maintains the ordering in which I add things (like ArrayList), but that
maps a key to a value.

I thought NameValueCollection was the right one - but it doesn''t
guarantee ordering.

If it doesn''t exist, why not? If it doesn''t what is the "right" way to
build it? Just have wrap your class around an instance of ArrayList
and have some internal class that takes the key given in the Add method
and instantiates this generic class with two members (a string key and
an object value), then add that to the internal ArrayList?

Thanks,
Novice

推荐答案

我会创建一个包装ArrayList和Hashtable的类。

每当有什么东西被添加到你的班级时,将它添加到ArrayList

和Hashtable。如果要返回迭代器,请将一个

迭代器返回到ArrayList中。如果你想按键查找,请在Hashtable中查找



I would create a class that wraps an ArrayList and a Hashtable.
Whenever anything is added to your class, add it to both the ArrayList
and the Hashtable. When you want to return an iterator, return an
iterator into the ArrayList. When you want a lookup by key, look it up
in the Hashtable.


我其实在想做类似的事情最初自

以来它都是基于参考的,会有最小的成本(内存)来做这样的事情 - 就是让ArrayList和Hashtable到

指向各种元素。


有谁知道为什么我应该扯掉PhoneNumberDictionary的

实现(那个从这里扩展DictionaryBase)


http://www.codeproject.com/csharp/collection1.asp


我不是在做PhoneNumberDictionary,但它似乎提供了我所提供的内容>
想要允许我指定任意名称和

任意对象。当我把它们全部拿走时,它们将按照我添加它们的顺序返回




听起来合适吗?

谢谢,

新手

I was actually thinking of doing something like that originally since
it is all reference based, there would be minimal cost (memory) to do
something like that - i.e. to have the ArrayList and the Hashtable to
point to the various elements.

Does anyone know of any reason why I should just rip off the
implementation of PhoneNumberDictionary (that extends DictionaryBase)
from here:
http://www.codeproject.com/csharp/collection1.asp

I''m not doing a PhoneNumberDictionary, but it seems to provide what I
want in terms of allowing me to specify an arbitrary name and an
arbitrary object. And when I get them all, they will be returned in
the order I added them.

Does that sound right?

Thanks,
Novice


Crud!


我''尝试过使用DictionaryBase,但编译器给了我

找不到DictionaryBase。我想这意味着它是一个.Net 2

级?


太糟糕了。

Crud!

I''ve tried using DictionaryBase, but the compiler gives me
DictionaryBase could not be found. I guess that means it is a .Net 2
class?

That sucks.

这篇关于想要一个维护订购的关键价值集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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