2012-02-18 7 views

ответ

7

Вопрос действительно зависит от того, какой тип 2D-массива вы думаете и какова размерная строка.

Правильное 2D массив

// Init 
var arr = new int[5,10]; 

// Counts 
arr.GetLength(0) // Length of first dimension: 5 
arr.GetLength(1) // Length of second dimension: 10 

Jagged "2D" массив

// Init 
var arr = new int[3][]; 
// Initially arr[0],arr[1],arr[2] is null, so we have to intialize them: 
arr[0] = new int[5]; 
arr[1] = new int[4]; 
arr[2] = new int[2]; 

// Counts 
arr.Length // Length of first dimension 
// In this case the "second dimension" (if you can call it that) is of variable size 
arr[0].Length // Length: 5 
arr[1].Length // Length: 4 
arr[2].Length // Length: 2 
+0

Спасибо, 2d массивы очень разные в C# – CodeKingPlusPlus

+0

@CodeKingPlusPlus Добро пожаловать :-) –

0

Это также будет работать,

string[,] a = new string[,] 
{ 
    {"ant", "aunt"}, 
    {"Sam", "Samantha"}, 
    {"clozapine", "quetiapine"}, 
    {"flomax", "volmax"}, 
    {"toradol", "tramadol"} 
}; 

// a[1,1] is equal to Samantha 
Смежные вопросы