c#如何在锯齿数组中创建字符串类型索引
本文关键字:创建 字符串 类型 索引 数组 | 更新日期: 2023-09-27 18:16:23
我的php代码如下
$array[$x] = array();
$array[$x]["no"] = $no;
$array[$x]["cid"] = $cid;
$array[$x]["wait"] = $wait;
$array[$x]["prio"] = $prio;
$array[$x]["debug"] = $command[$i];
x + +;
问题:我想在c#中有类似的代码,一切都很好,但在参差不齐的数组步骤中,我很困惑如何在c#中这样做,有人能在这方面帮助我吗?这是一个数组的索引是字符串值。
实现锯齿数组的一种方法是使用字典。基于下面代码的一些东西可能会让你开始。
Dictionary<string, string>[] array;
void MyMethod(int[] ckeys, int gotovalue, string[] command)
{
int x = 0;
for(int ii = (ckeys[0] + 1); ii < gotovalue; ii++)
{
string no = preg_replace(" .*", "", command[ii]);
string temp = preg_replace("^[0-9]*. ", "", command[ii]);
string cid = preg_replace(" (.*", "", temp);
temp = preg_replace(".* (wait: ", "", command[ii]);
string wait = preg_replace(",.*", "", temp);
temp = preg_replace(".*, prio: ", "", command[ii]);
string prio = preg_replace(").*", "", temp);
array[x] = new Dictionary<string, string>();
array[x]["no"] = no;
array[x]["cid"] = cid;
array[x]["wait"] = wait;
array[x]["prio"] = prio;
array[x]["debug"] = command[ii];
x++;
}
}
string preg_replace(string aa, string bb, string cc)
{
return aa + bb + cc;
}
编辑:我将问题的初始版本中的代码转换为c#,假设所有未指定的类型都是字符串。被调用的例程preg_replace
没有指定,但它似乎接受三个字符串并返回一个。
原来的问题有$x = 0;
行,它似乎将$x
定义为一个整数并初始化它。$array[$x] = array();
行似乎表明,给定整数索引处的$array
指向一个空数组。然后$array[$x]["no"]
行将该数组的"no"
元素设置为字符串。我建议的c#将array
声明为一个字典数组。c#字典是关联数组的一种形式,在Perl语言中它被称为"哈希"。整段代码将值写入结构中,有效地将参数中找到的值初始化为MyMethod
。
其他地方将需要一个语句,如array = new Dictionary<string, string>[gotovalue]
,使array
引用一个实际的数组