D: Arrays¶
Concepts in Action¶
Understanding arrays is fundamental for storing and managing data in your games.
-
Storing Bullets: In a shooting game, you might have a gun that can hold a certain number of bullets at a time. You could use an array to store these bullets. Each element in the array could be an instance of a Bullet class, and you could use the length of the array to determine how many bullets the gun currently holds.
-
Tracking Enemies: In an adventure game, you might have multiple enemies on the screen at once. You could use an array to keep track of all these enemies. Each element in the array could be an instance of an Enemy class, and you could loop through the array to update or draw each enemy.
-
Level Progression: In a platformer game, you might have a sequence of levels that the player needs to complete. You could use an array to store these levels. Each element in the array could be an instance of a Level class, and you could use the index of the current level in the array to control progression through the game.
Arrays provide a way to store, access, and manipulate multiple pieces of data under a single variable.
Introduction¶
An array in C# is a collection of elements of the same type. It is a numbered list of items where each item can be accessed by its index (a number representing its position in the list). Arrays are particularly useful when you want to work with a collection of values, such as a list of student names or a sequence of numbers.
Prior C# knowledge required¶
Understanding basic C# concepts like variables, classes, types, and loops will be helpful.
Definition¶
An array is a data structure that can store a fixed-size sequence of elements of the same type. In other words, an array can hold more than one value at a time, but it can only hold values of the same type. The values are stored at contiguous memory locations. This means that the memory used to store the array’s values is one unbroken block. This feature makes arrays efficient, but it also means that the size of an array can’t be changed once it’s created.
In C#, arrays are reference types and can be declared in various ways. Here are a few examples:
int[] array1 = new int[5]; // Declares an array of integers that can hold 5 elements.
string[] array2 = new string[3]; // Declares an array of strings that can hold 3 elements.
bool[] array3 = new bool[10]; // Declares an array of booleans that can hold 10 elements.
Cat[] array4 = new Cat[4]; // Declares an array of objects of type Cat, that can hold 4 elements.
You can also create an array and initialize it with values at the same time:
Differences to JavaScript¶
JavaScript arrays are more flexible than C# arrays. In JavaScript, you can store different types of elements in the same array and you can easily add and remove elements. C# arrays, on the other hand, can only contain elements of the same type and their size is fixed at the time of creation.
Similarities to JavaScript¶
Both C# and JavaScript use zero-based indexing for arrays, which means the first element is at index 0. They both also allow you to access and modify elements via their index.
Examples¶
// Declare and initialize an array
int[] scores = {85, 90, 78, 92, 88};
// Access an element in the array
int firstScore = scores[0]; // 85
// Modify an element in the array
scores[2] = 80;
// Loop through the array
for (int i = 0; i < scores.Length; i++)
{
Debug.Log(scores[i]);
}
Key Takeaways¶
- Fixed Size: Once an array in C# is created, its size can’t be changed. The length of the array is determined at the time of creation.
- Same Data Type: All elements in a C# array must be of the same type. This is different from languages like JavaScript, where arrays can contain elements of different types.
- Zero-Indexed: Arrays in C# are zero-indexed. This means that the first element is at index 0, the second element is at index 1, and so on.
- Memory Efficiency: Because arrays are stored in contiguous memory locations, they can be more efficient than other data structures for storing and accessing sequences of data.
- Element Access: Elements in an array can be accessed directly using their index. This allows for fast access to data but also means you need to be careful to avoid accessing outside the bounds of the array.
- Array Initialization: In C#, you can create an array and initialize its elements at the same time. You can also create an empty array and assign values to its elements later.