简体   繁体   中英

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:

在此处输入图片说明

If anyone could help it would be much appreciated.

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" } };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM