CHAPTER 06
Beginner
Arrays, Lists, and Collections
Updated: May 16, 2026
25 min read
# CHAPTER 6
Arrays, Lists, and Collections
1. Introduction
A variable holds one piece of data.string weapon = "Sword";. But what if your player has a backpack that holds 20 weapons? Creating 20 individual variables (weapon1, weapon2, etc.) is horrible programming practice. To store massive groups of data, we use Data Structures. In C#, the three most common structures used in game development are Arrays, Lists, and Dictionaries. In this chapter, we will master Collections. We will learn how to store fixed groups of items in Arrays, create dynamic expanding inventories using Lists, and map key-value pairs using Dictionaries.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Declare, initialize, and access data in an
Array.
- Understand the Zero-Index rule of programming.
-
Use a
List<T>to create dynamic, resizable collections.
-
Use a
Dictionary<TKey, TValue>for ultra-fast data lookups.
-
Iterate through collections using
foreachloops.
3. Arrays (The Fixed Box)
An Array is a collection of variables of the *same type* stored in a continuous block of memory.- *Limitation:* You must define exactly how big the array is when you create it. It cannot grow or shrink later!
- Arrays are incredibly fast, making them perfect for fixed data (e.g., 4 players in a split-screen game, or 6 slots in a revolver).
csharp
4. Lists (The Expanding Backpack)
A List is the most commonly used collection in modern game dev. Unlike Arrays, Lists can dynamically grow and shrink. If a player picks up 50 apples, the List simply expands.-
*Requirement:* You must add
using System.Collections.Generic;at the top of your script.
csharp
5. Dictionaries (The Key-Value Map)
What if you want to look up an enemy's health by their name? Searching through a List of 1,000 enemies takes time. A Dictionary solves this by pairing a "Key" with a "Value."
csharp
6. The foreach Loop
To read every item in a List or Array, you *could* use a standard for loop, but C# provides a much cleaner tool: the foreach loop.
csharp
7. Visual Learning: The Index Concept
txt
*Note: If you try to access inventory[4], the game will instantly crash with an "Index Out Of Bounds" exception!*
8. Best Practices
-
Use Lists for Gameplay, Arrays for Performance: If you are spawning and deleting enemies constantly, use a
List. If you are building a voxel engine (like Minecraft) holding exactly 16x16x16 blocks, use anArray. Arrays are slightly faster for the CPU to read.
9. Common Mistakes
-
Index Out of Range Exception: The most common bug in programming. If an Array has 3 items, the slots are
0, 1, 2. If you ask the computer for slot3, it looks outside the allocated memory and crashes the program. *Always remember: Length is 3, but the highest Index is 2!*
10. Mini Project: Build an RPG Inventory
Objective: Create a dynamic List and iterate through it.- 1. Open your Console project. Write the following:
csharp
- 2. Press Play. Watch the dynamic addition, removal, and rendering of the data!
11. Practice Exercises
-
1.
What namespace must be included at the top of your script to use
List<T>andDictionary<TKey, TValue>?
-
2.
If you declare
int[] scores = new int[5];, what is the exact index number of the very last slot in that array?
12. MCQs with Answers
Question 1
You are programming an RPG where the player's backpack can hold anywhere from 0 to 999 items depending on what they pick up. Which data structure is best suited for this?
Question 2
You have a List<string> names containing 3 items. What happens if you try to execute Console.WriteLine(names[3]);?
13. Interview Questions
-
Q: Explain the fundamental difference in memory allocation between an
Arrayand aList<T>in C#. When would you explicitly choose an Array over a List?
-
Q: Walk me through the concept of "Zero-Index" arrays. Why does
array.Lengthreturn a number that is 1 higher than the maximum readable index?
- Q: A game requires you to instantly look up the required crafting materials for an item simply by providing the item's name (e.g., passing "Iron Sword" instantly returns "2 Iron, 1 Wood"). Which Collection is best suited for this, and why?
14. FAQs
Q: Can a List hold custom Objects? A: Yes! A List can hold anything.List<Enemy> activeEnemies = new List<Enemy>(); is incredibly common. You can then use a foreach loop to call .TakeDamage() on every single enemy in the list at once!
15. Summary
In Chapter 6, we learned how to manage vast amounts of game data. We explored Arrays for strict, fast, fixed-size data blocks. We unlocked the versatility of Lists to build dynamic, expanding inventories. We utilized Dictionaries for lightning-fast key-value lookups, and mastered theforeach loop to easily iterate through our collections. We no longer write 50 variables; we write one List.