Multidimensional arrays in C #

I defined this multidimensional array in C #:

int[,] values = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

Now the values ​​are no longer in this form, but in the dictionary:

values2.Add("KeyA", new List<float> {1,4});
values2.Add("KeyB", new List<float> {2,5});
values2.Add("KeyC", new List<float> {3,6});

Now I'm trying again to parse this dictionary into a two-dimensional array, but for some reason there are problems:

List<float> outList = new List<float>();
values2.TryGetValue(values2.Keys.ElementAt(0) as string, out outList);

int[,] values = new int[outList.Count, values2.Keys.Count];

for (int i = 0; i < values2.Keys.Count; i++)
{
   List<float> list = new List<float>();
   values2.TryGetValue(values2.Keys.ElementAt(i), out list);

   for (int j = 0; j < list.Count; j++)
   {
       values[i, j] = Convert.ToInt32(list.ElementAt(j));
   }
}

This throws an InnerException: System.IndexOutOfRangeException. But why? I cannot convert values ​​correctly.

Edit: it can be assumed that all values ​​in the dictionary have the same list length. I check it somewhere else.

+3
source share
1 answer

I think this is as simple as getting your length or indexing in the wrong way. Here is your array creation:

int[,] values = new int[outList.Count, values.Keys.Count];

values[i, j], i metrics.Keys.Count j list.Count.

, values[j, i]. , , , .

+5

Source: https://habr.com/ru/post/1775151/


All Articles