Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET and direct object collections

Tags:

c#

collections

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.

like image 480
Gelion Avatar asked Jan 18 '26 20:01

Gelion


2 Answers

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];
like image 186
Marco Avatar answered Jan 20 '26 11:01

Marco


Use a dictionary.

Dictionary<int, YourType>

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

like image 21
Guillaume Avatar answered Jan 20 '26 12:01

Guillaume