Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"]);
share|improve this question

4 Answers

up vote 8 down vote accepted

Are you sure it wouldn't be more appropriate to create a class/struct to contain the data? For example:

class Person
{
    public string FirstName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public string Phone
    {
        get;
        set;
    }
}

Then you'd just create an array of Person:

var array = new Person[1];
array[0] = new Person() { FirstName = "Joe", LastName = "Smith", Phone = "foo" };

Or, since you say "flexible size", maybe you'd want a list instead:

var list = new List<Person>();
list.Add(new Person());

Update: The syntax used to set array[0] in the first example is an object initializer; the following two snippets are roughly equivalent:

var foo = new Person();
foo.FirstName = "John";

var bar = new Person() { FirstName = "John" };

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:

var john = new Person() { FirstName = "John" };
var joe = new Person() { FirstName = "Joe" };
var list = new List<Person>() { john, joe };
share|improve this answer
var list = new List<Person>(); list.Add(new Person()); very interesting how to add the values? list.Add(new Person(){ FirstName = "Joe", LastName = "Smith", Phone = "foo" }); like this? – Data-Base May 4 '10 at 11:14
@Data-Base: I've updated my answer to clarify. – Will Vousden May 4 '10 at 11:28

You can simply use this:

Dictionary<int, Dictionary<string, string>>

Use it like this:

var dd = new Dictionary<int, Dictionary<string, string>>();
dd[5] = new Dictionary<string, string>();
dd[5]["a"] = "foo";

You can also create a new class to simplify the creation of the inner dictionary:

class DDict { // optional: generic
    private readonly Dictionary<int, Dictionary<string, string>> _Inner = new ...;

    public Dictionary<string, string> this (int index) {
        Dictionary<string, string> d;
        if (!_Inner.TryGetValue(index, out d)) {
             d = new Dictionary<string, string>();
             _Inner.Add(index, d);
        }
        return d;
    }
}

var dd = new DDict();
dd[5]["a"] = "hi";

If the first index is sequential, you can of course also just use an array of dictionaries:

var dd = new Dictionary<string, string>[128];

Also, if the inner members are always the same, I suggest to create a new class and access it in an array:

class Dat {
    string name;
    string phone;
}
var list = new Dat[128]

// access:
list[5].name = "matt";

Instead of an array, you could also use a List or a Dictionary<int, Dat> in that case.

share|improve this answer
+1 from me for reading the question correctly lol – Grokodile May 4 '10 at 9:02

I don't believe you can do this with an Array unless you have a single Array of KeyValuePair<int,string>, but I think you really want a Dictionary<int,string>.

var dic = new Dictionary<int,string>();
dic[0] = "zero";
dic[1] = "one";
dic[2] = "two";

foreach(KeyValuePair<int,string> kvp in dic)
{
   Console.WriteLine(String.Format("Key: {0}, Value: {1}",kvp.Key,kvp.Value);

}
share|improve this answer
Oh, sorry I didn't pick up on the 2 keys part. – Grokodile May 4 '10 at 9:00

Actually i just see two dimensions. The first one is a row index, the second is a column index. And this sounds like a DataTable to me.

share|improve this answer
in that case in this example dotnetperls.com/datatable-foreach how to show the item label{name,phone} instead of "item:" cheers – Data-Base May 4 '10 at 11:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.