It seems to me that a dictionary is enough for what you need.
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
var dicionario = new Dictionary<string, MyObject> {
["item1"] = new MyObject("item1", "valor1", 1, 1.1),
["item2"] = new MyObject("item2", "valor2", 2, 2.2),
["item3"] = new MyObject("item3", "valor3", 3, 3.3)
};
Console.WriteLine(dicionario["item2"].Value);
}
}
public class MyObject {
public string Name {get; set;}
public string Value {get; set;}
public int Prop1 {get; set;}
public double Prop2 {get; set;}
public MyObject (string name, string valueParam, int prop1, double prop2){
this.Name = name;
this.Value = valueParam;
this.Prop1 = prop1;
this.Prop2 = prop2;
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
The dictionary has access time virtually identical to that of the array, (at least the complexity of access is essentially the same (O(1) in typical case, but O(n) in worst case, which in practice never comes close to happening).
It is important to note that if you have all the elements listed they can come in any order and if you try to take an element by its index it may not see what you are expecting, the algorithm is not stable to give this possibility, even if it works on a basic test, will not work in another case, and the same test that worked may not work anymore if you change the Dictionary
or the algorithm of hash of the object used as key. There is no guarantee of anything.
If you need order use a SortedDictionary
. It is slightly slower, but in most cases changes very little and there are many cases that it may be faster than the dictionary. It has O(log n) complexity that is very close to O(1). There are cases that the formula of hash may add overhaed that makes it worse than access to the tree of SortedDictionary
. His worst case is O(log n), so he is much more predictable and avoids certain types of DOS attacks than the hash allows. And because of the location of reference impaired the table hash of Dictionary
can access the data more slowly by not enjoying the cache well.
If you have to ensure that key values do not recur can use a Set
, have a few options.
There is another option, the KeyedCollection
which gives you the advantage of the list and the advantage of the dictionary at the same time, perhaps it is the most suitable for what you want. It uses one of the members of the object as key implicitly.
Needs to be array or can you change the data structure used? Do the elements in it need to be in a specific order? Can you have repeated elements in the first member of the object? You can separate that member from the rest of the object?
– Maniero
Use a data dictionary
– novic
I have a preference for
array
, but if that’s not possible, I can consider other options as well. Well, I don’t currently need the elements in specific order, but is there any solution that makes it possible to sort them? I didn’t understand the last questions @Maniero– Jedaias Rodrigues
How could I use a data dictionary in this case @Virgilionovic ?
– Jedaias Rodrigues
@Jedaiasrodrigues I made an example...
– novic