blobbymatt blobbymatt - 2 months ago 10
C# Question

instantiate and initialize multi dimensional array C#

I have a multi dimensional array but i want to be able to instantiate and initialize it in one line does anyone know how to do this?

Here is what i have at the moment.

int[,] Columns = [3,2];
Columns[0,0]= 1;
Columns[1,0]= 0;
Columns[2,0]= 2;
Columns[0,1]= "Distinct";
Columns[1,1]= "Sum";
Columns[2,1]= "Distinct";


im trying to get somthing along the lines of:

enter image description here

If anyone could help it would be much appreciated.

Answer

You can, you can use something called a collection initializer with rectangular arrays! But you can't declare the array type as int and try and store strings (such as "Sum") in it too.

You could use the object type to store different data types in the same collection:

object[,] Columns = { { 1, 0, 2 }, { "Distinct", "Sum", "Distinct" } };