5

I just want get a 2 dimension array of List in c#. In my thought, below should works but it didn't, the 1 dimension array of List works. I use Unity3D with Mono, however I think it's language related problem.

List<MyType>[,] twoDArrayofList;//don't work, it's type is List<MyType>
List<MyType>[] oneDArrayofList;//it's works, the type is List<MyType>

Anyone know what's wrong? Thank you.

Matrix Bai
  • 1,097
  • 3
  • 11
  • 20

2 Answers2

6

What do you mean by "doesn't work"? What error are you getting?

I don't know about Mono (or Unity3D), but I just tried the following in a .NET project and got the results I expected:

List<string>[,] strings = new List<string>[2, 2];
strings[0, 0] = new List<string> { "One" };
strings[0, 1] = new List<string> { "Two" };
strings[1, 0] = new List<string> { "Three" };
strings[1, 1] = new List<string> { "Four" };
Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • 2
    I figure it out, it's the the IDE MonoDeveloper error, the IDE can't recognize this situation, and give me a wrong type, what a shame. Thanks for your answer let me start checking the IDE – Matrix Bai Apr 08 '12 at 08:25
0

This is an older post, but it came up in my search for a similar answer. I found that creating a Dictionary instead of a List meets my needs. You can't iterate by index, but if you have a discrete list and you are iterating through the first string "Key", you can easily obtain the second string "Value".

Here is a stackoverflow post with examples: What is the best way to iterate over a Dictionary in C#?

Community
  • 1
  • 1
CigarDoug
  • 1,438
  • 1
  • 15
  • 29