我在 C# 中创建了一个二维数组的哈希表,但无法弄清楚如何直接访问数组值,以下是我当前的代码:
// create the hashtable
Hashtable hashLocOne = new Hashtable();
// add to the hashtable if we don't yet have this location
if (!hashLocOne.ContainsKey(strCurrentLocationId))
hashLocOne.Add(strCurrentLocationId,new double[20, 2] { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } });
// add to the value at a given array position, this does not work
hashLocAll[strReportLocationId][iPNLLine, 0] += pnl_value;
最佳答案
Hashtable 不知道里面存储的是什么类型的对象;你必须手动转换每一个:
double result = ((double[,]) table["foo"])[4][5];
如果可能,您应该使用字典而不是哈希表:
var dict = new Dictionary<String, double[,]>();
double result = dict["foo"][4][5];
关于c#访问二维数组的哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/638548/