c#访问二维数组的哈希表

tmc*_*han 1 c#

我在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;
Run Code Online (Sandbox Code Playgroud)

Joh*_*lla 5

Hashtable不知道它中存储了哪种对象; 你必须手动施放每一个:

double result = ((double[,]) table["foo"])[4][5];
Run Code Online (Sandbox Code Playgroud)

如果可能,您应该使用Dictionary而不是Hashtable:

var dict = new Dictionary<String, double[,]>();
double result = dict["foo"][4][5];
Run Code Online (Sandbox Code Playgroud)