如何在c#中创建多维数组?
我想分配这样的值:
myArr["level1"]["enemy"][0] = 1;
myArr["level1"]["enemy"][1] = 4;
myArr["level1"]["friend"][0] = 2;
myArr["level1"]["friend"][1] = 3;
我可以使用
执行普通数组public Array level1;
并将值推送到它。
但我似乎无法做多维的
答案 0 :(得分:2)
我认为你在C#中使用的最类似的东西是Dictionary
:
Dictionary<Person, string> dictionary = new Dictionary<Person, string>();
Person myPerson = new Person();
dictionary[myPerson] = "Some String";
...
string someString = dictionary[myPerson];
Console.WriteLine(someString); // "Some String"
您可以使用Dictionary并将某种元组结构作为关键字:
public class TwoKeyDictionary<K1,K2,V>
{
private readonly Dictionary<Pair<K1,K2>, V> _dict;
public V this[K1 k1, K2 k2]
{
get { return _dict[new Pair(k1,k2)]; }
}
private struct Pair
{
public K1 First;
public K2 Second;
public override Int32 GetHashCode()
{
return First.GetHashCode() ^ Second.GetHashCode();
}
// ... Equals, ctor, etc...
}
}