Skip to content

C# Arrays

myArray.Length returns size of array. It's property, not a method so no parentheses. Index starts at 0

value types vs reference types

  • value - set to default value
  • reference types need to be instantiated
  • remember - structs are values and so will take up space
  • arrays themselves are reference types

Rectangular Arrays

int [,] matrix = new int[3,3];

.GetLength(0) - returns size of 1st dimension
.GetLength(1) - returns size of 2nd dimension

Jagged Arrays

int [][] matrix = new int [3][];
for(int i = 0; i< matrix.Length; i++) {
    matrix[i] = new int[i+1];
}