C# .NET 和直接对象集合
嘿,有没有像 arrayList 这样的集合类型,我可以使用 ID 添加对象?
有效地,因为我的帖子标题暗示了直接对象集合。例如:
DirectCollection.addAt(23, someobject);
等等
DirectCollection.getAt(23);
我知道 arraylist 在这种情况下是可用的,但我必须使用空引用对象生成初始条目,
如果该对象具有像 23 这样的 ID,我必须生成 22 个其他条目只是为了添加它显然是不切实际的。
基本上使用对象位置值作为唯一 ID。
有什么想法吗?
非常感谢。
Hey all is there a collection type like arrayList which i can add an object to using an ID?
effectively as the title of my post sugests a Direct object collection. so for example:
DirectCollection.addAt(23, someobject);
and
DirectCollection.getAt(23);
etc etc
i know arraylist is usable in that case but i have to generate the initial entry with a null reference object and if if the object has an ID like 23 i have to generate 22 other entries just to add it which is clearly impractical.
basically using the object position value as a unique ID.
Any thoughts?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Dictionary
像这样:
You could use
Dictionary<int, YourType>
Like this:
使用字典。
它允许您使用给定键和非连续范围添加/获取/删除项目。
Use a dictionary.
It allows you to add/get/remove items with a given key and non continuous ranges.
您可以使用 Dictionary
您的示例的代码非常简单:
You could use a Dictionary
The code for your example would be very simple:
我认为 KeyedCollection 或 Dictionary 就是您所需要的。
I think KeyedCollection or Dictionary is what you need.
使用System.Collections.Hashtable。它允许存储异构类型的对象(
Hashtable
可以保存多种类型的对象)。例子:
Use
System.Collections.Hashtable
. It allow to store the heterogeneous type of object (aHashtable
can hold the multiple type of object).Example: