Tuesday

Today:

Arrays


const int n=50;

// Syntax to Initialize an Array
aType[] VariableName = new aType[n];

// Array of Integers
int[] myListOfInts = new int[20];

// Array of Integers initialized with values
int[] myArray = new int[5] {1, 2, 3, 4, 5};

// One Dimensional Array
GameObject[] myListX = new GameObject[50];
myListX[0].transform.position ...  // This will give error

// Two Dimensional Array
GameObject[,] myListXY = new GameObject[50, 50];

// One Dimensional Array of Colors (Struct)
Color[] myColors = new Colors[20];
myColors[3].r = 0.7f;

Array of Objects vs Arrays of Ints, Floats, Structs

If you have an array of ints, floats, structs, you do not need to initialize the data of the array. If you have an array of objects, you need to initialize the data(object).

We will dive into Structs(structures) more when we cover Objects.

Lists

In C#, both arrays and lists are used to store data, but they have some key differences:

  1. Size: An array has a fixed size. Once you define the size of an array, it cannot be changed. However, a list is dynamic and can grow or shrink as needed.