I’m trying to figure out how to build a multi-dimensional “array” that is:
- flexible size
- use 2 keys
- 1st key is int (flexible)
- 2nd key is string (kind of limited)
The use will be like:
console.writelen(array[0]["firstname"]);
console.writelen(array[0]["lastname"]);
console.writelen(array[0]["phone"]);
console.writelen(array[1]["firstname"]);
console.writelen(array[1]["lastname"]);
console.writelen(array[1]["phone"]);
.....
.....
console.writelen(array[x]["firstname"]);
console.writelen(array[x]["lastname"]);
console.writelen(array[x]["phone"]);
Are you sure it wouldn’t be more appropriate to create a class/struct to contain the data? For example:
Then you’d just create an array of
Person:Or, since you say “flexible size”, maybe you’d want a list instead:
Update: The syntax used to set
array[0]in the first example is an object initializer; the following two snippets are roughly equivalent:So yes, you could just call
list.Add(new Person() { ... })if you wanted to. You could also make use of collection initializers in this case: