1

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#?

2
  • What does that mean: "I want to use something dynamic"? Commented Aug 27, 2016 at 1:14
  • @Gaber-ber defined at runtime. Commented Aug 27, 2016 at 1:15

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.