Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-动态创建索引数量可变的数组_C#_Arrays - Fatal编程技术网

C#-动态创建索引数量可变的数组

C#-动态创建索引数量可变的数组,c#,arrays,C#,Arrays,我有一个程序使用: // the assignments of hardcoded ids and memoryIndex are for the sake of this question int id1 = 1; int id2 = 2; int id3 = 3; int id4 = 4; int memoryIndex = 0; int[][][] sequence; sequence[id1][id2][id3] = memoryIndex; 我想使用动态的东西,在运行时定义不同数

我有一个程序使用:

// the assignments of hardcoded ids and memoryIndex are for the sake of this question
int id1 = 1;
int id2 = 2;
int id3 = 3;
int id4 = 4;
int memoryIndex = 0;

int[][][] sequence;

sequence[id1][id2][id3] = memoryIndex;

我想使用动态的东西,在运行时定义不同数量的键,这样我也可以有4个或更多属于一个序列的ID。我付不起很多支票,因为速度比记忆更重要。如何在C#中实现这一点?

我想说,看看C#Array类。它有几种创建多维数组的方法。例如,Array.CreateInstance方法(类型, Int32[]),其中第二个参数是一个数组,表示每个维度的长度。使用您的代码的示例如下所示:

int id1 = 1;
int id2 = 2;
int id3 = 3;
int id4 = 4;

int[] dimensions = {2, 3, 4, 5};
int memoryIndex = -1;

var sequence = Array.CreateInstance(typeof(int), dimensions);

sequence.SetValue(memoryIndex, new int[]{id1, id2, id3, id4});

这里有一个指向文档的链接:

这是什么意思:“我想使用动态的东西”@Gaber-ber在运行时定义。