C# .NET 和直接对象集合

发布于 2024-12-07 05:54:35 字数 378 浏览 0 评论 0原文

嘿,有没有像 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半寸时光 2024-12-14 05:54:35

您可以使用 Dictionary
像这样:

var p = new Dictionary<int, YourType>();
p.Add(23, your_object);
YourType object_you_just_added = p[23];

You could use Dictionary<int, YourType>
Like this:

var p = new Dictionary<int, YourType>();
p.Add(23, your_object);
YourType object_you_just_added = p[23];
最美不过初阳 2024-12-14 05:54:35

使用字典。

Dictionary<int, YourType>

它允许您使用给定键和非连续范围添加/获取/删除项目。

Use a dictionary.

Dictionary<int, YourType>

It allows you to add/get/remove items with a given key and non continuous ranges.

新雨望断虹 2024-12-14 05:54:35

您可以使用 Dictionary

您的示例的代码非常简单:

Dictionary<int, AType> directCollection = new Dictionary<int, AType>();
directCollection.Add(23, someObjectOfAType);
AType anObject = directCollection[23];

You could use a Dictionary

The code for your example would be very simple:

Dictionary<int, AType> directCollection = new Dictionary<int, AType>();
directCollection.Add(23, someObjectOfAType);
AType anObject = directCollection[23];
很酷不放纵 2024-12-14 05:54:35

我认为 KeyedCollection 或 Dictionary 就是您所需要的。

I think KeyedCollection or Dictionary is what you need.

最笨的告白 2024-12-14 05:54:35

使用System.Collections.Hashtable。它允许存储异构类型的对象(Hashtable可以保存多种类型的对象)。

例子:

System.Collections.Hashtable keyObjectMap = new System.Collections.Hashtable();
//Add into Hashtable
keyObjectMap["Key_1"] = "First String";
keyObjectMap["Key_2"] = "Second String";
//Add the value type
keyObjectMap["Key_3"] = 1;
keyObjectMap["Key_4"] = new object();

//Get value/object from Hashtable
string value = (string)keyObjectMap["Key_2"];
int intValue = (int)keyObjectMap["Key_3"];

Use System.Collections.Hashtable. It allow to store the heterogeneous type of object (a Hashtable can hold the multiple type of object).

Example:

System.Collections.Hashtable keyObjectMap = new System.Collections.Hashtable();
//Add into Hashtable
keyObjectMap["Key_1"] = "First String";
keyObjectMap["Key_2"] = "Second String";
//Add the value type
keyObjectMap["Key_3"] = 1;
keyObjectMap["Key_4"] = new object();

//Get value/object from Hashtable
string value = (string)keyObjectMap["Key_2"];
int intValue = (int)keyObjectMap["Key_3"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文