Arrays And Lists

Two-dimensional Arrays

There are 2 types of multi-dimensional arrays: rectangular and jagged.

To create a rectangular multi-dimensional array, add the row and column values in your array initialization.

var matrix = new int[3, 5]; // 3 rows, 5 columns

// Accessing items
matrix[0, 0];

To create a jagged multi-dimensional array, define the sub-arrays after initiazliation.

var matrix = new int[3][]; // 3 rows, unspecified columns

matrix[0] = new int[3]; // 1st row has 3 columns
matrix[1] = new int[5]; // 2nd row has 5 columns
matrix[2] = new int[4]; // 3rd row has 4 columns

// Accessing items
matrix[0][1];

Lists

Lists have a dynamic size. You use them when you don't know how many items you'll be storing ahead of time.

var numbers = new List<int>();

Pro tip: 99% of the time, you will be using lists and not arrays.

Last updated