Home » Unity Arrays

Arrays

An array is used to store a sequential collection of values of the same type. In short, an array is used to store lists of values in a single variable. Suppose if you want to store a number of player names, rather than storing them individually as string p1, string p2, string p3, etc. we can store them in an array.

Arrays are a way of storing a collection of data of the same type together.

Declaring an array

To declare an array in C#, you must first say what type of data will be stored in the array. After the type, specify an open square bracket and then immediately a closed square bracket, []. This will make the variable an actual array. We also need to specify the size of the array. It simply means how many places are there in our variable to be accessed.

Syntax:

Example:

To allocate empty values to all places in the array, simply write the “new” keyword followed by the type, an open square bracket, a number describing the size of the array, and then a closed square bracket.

Example

Let’s see one simple example using an array:

Output:

Arrays


You may also like