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

标签 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;

最佳答案

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/

相关文章:

c# - 检查文件是否存在并且在 C# 中具有正确的命名标准

c# - 如何获得系统权限

c# - 为什么 Office Interop 引用在 VS 2010 中显示错误

c# - 为什么不触发 MouseEnter 事件?

c# - 这种方法能达到100%的代码覆盖率吗?

c# - 从 control.Invoke((MethodInvoker) delegate {/* ... */} 返回值;我需要一些解释

c# - 使用 ModuleBuilder 将类标记为 `internal static`

c# - 每次启动 ASP.NET Core MVC 应用程序时出现 IIS 错误

c# - 无状态 WCF 服务和数据库连接池

c# - 如何从 ASP.NET WebApi 中的对象中排除某些属性