Reputation: 672
I have a program that uses:
// 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;
I want to use something dynamic with a variable amount of keys defined at runtime, so I could also have 4 or more ids that belong to a sequence. I can't afford a lot of checks because speed is a greater concern than memory. How could I accomplish this in C#?
Upvotes: 1
Views: 33
Reputation: 964
I would say take a look at the C# Array class. It has several methods for creating arrays with multiple dimensions. For example, Array.CreateInstance Method (Type, Int32[]) where the second parameter is an array indicating the length of each dimensions. A sample using your code would look like this:
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});
Here's a link to the documentation: https://msdn.microsoft.com/en-us/library/dfs8044k(v=vs.110).aspx
Upvotes: 2