is it possible to make an array or a list or something equal with a custon integer key?
e.g.
I want to have an array of size 3 but not with the开发者_高级运维 default keys 0,1,2 but with custon keys like: 100,302,502What you're looking for is a Dictionary.
var dic = new Dictionary<int, int>();
dic.Add(100, 35);
dic.Add(302, 45);
dic.Add(502, 55);
Console.WriteLine(dic[100]);
Console.WriteLine(dic[502]);
This will output 35 and 55.
You can also run a foreach
over the dictionary, but since it's a hash table, it won't necessarily be in order. Also, while the performance is constant time (i.e. won't be too much slower even if the dictionary gets really big), it is somewhat slower than an array.
Edit: By the way, you can also use different kinds of values for the key and/or value. For instance, you can say something like:
var dic = new Dictionary<string, double>;
dic.Add("hot dogs", 3.99);
Console.WriteLine(dic["hot dogs"]);
And it'll print 3.99
.
Have a look at Dictionary<T>
or HashTable.
You could have a 2 dimensional array.
精彩评论