HEIGHT ABOVE SEA LEVEL
  • Articles
  • Quizzes
Join Patreon

Suggested

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon
    • Articles
    • Quizzes
    Join Patreon

    Here's some dummy text to automatically resize the container as if there were actual text

    Some dummy text that's hidden to resize the component automatically

    Performance

    Audio

    Become a patreon member and receive the following:

    HEIGHT ABOVE SEA LEVEL
    • Articles
    • Quizzes
    Join Patreon

    Suggested

    • constants headerA Game Developer's Guide to Constants in C#December 12, 2025
    • A reflection probe in front of a house in Unity.Reflection Probes in Unity: How to Get the Most Out of ThemNovember 2, 2025
    • How to Bake Lights in Unity: A Step-by-Step GuideNovember 2, 2025
    • strings header imageA Game Developer's Guide to Strings in C#December 12, 2025
    • A Game Developer's Guide to Loops in C#December 12, 2025

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon
    • Articles
    • Quizzes
    Join Patreon

    Question of the Day

    What is the value of the isOnline variable in this C# code?

    var isOnline = true && false;

    Explanation

    When using the && operator, ALL boolean values must be true for the final value to be true. If even one of them is false, the final value will always be false.

    Struggling with operators in C#? Click here to learn how to use them

    Think you know game dev? Check out the quizzes and find out.

    A Game Developer's Guide to Lists and Arrays in C#

    A Game Developer's Guide to Lists and Arrays in C#

    • Beginner C#
    • Set Up & Getting Started
    Victor Nyagudi
    Victor Nyagudi

    December 12, 2025

    Read this article ad-free by becoming a Patreon member

    Inventory items. Weapons carried. Checkpoints. These are all groups of related items in a game you should store together, and lists and arrays are the perfect data structures for that.

    Lists and arrays in C# are examples of collections that store items. They save you the trouble of manually creating variables for each item.

    For example, without collections, you'd have to manually create each inventory item and keep track of it, a very tedious and unpleasant experience.

    Lists and arrays group items and provide mechanisms for adding, removing, and updating them.

    This article assumes you're familiar with classes, loops, and methods because those are the areas you'll use lists and arrays most.

    If you're a visual learner, scroll to the bottom for the video version, or you can watch it on YouTube.

    How to Use Arrays in Unity

    Arrays are a collection in C# that store a fixed number of items. The total number of items is specified during initialization and limits the maximum number of items you can include in the array.

    Once you've created an array, you can't add or remove items from it. You can only update the existing items to new ones.

    A specified type followed by an opening and closing square bracket signifies an array in C#, e.g., int[] or Weapon[].

    An array can store items of different types, e.g., int, bool, string, or classes you created. You can even have an array that stores other arrays.

    There are multiple ways to create array variables, as shown below.

    var checkpoints = new byte[10]; // <- "empty" array.
    
    var inventoryItems = new string[] { "Wood", "Steel" };
    
    int[] numbers = [1, 2, 3, 4, 5];
    
    Weapon weapons =
    {
      // Remember class constructors??
      new Weapon("Machete"),
      new Weapon("Steel Baton"),
      new Weapon("Kukri")
    };

    You'll need to use the new keyword to create an array instance since arrays inherit from the pre-existing Array class.

    You can even use arrays as fields and properties, method return types, or method parameters.

    public class Player
    {
      private string[] _inventoryItems; // <- Array field.
    
      // A method with an array parameter.
      public void DiscardItems(string[] items)
      {
        Debug.Log($"{items.Length} have been discarded.");
      }
    }

    You can also create an "empty array" with a specified number of slots but no defined items.

    "Empty arrays" could be helpful if you want to update the items later. The array isn't technically empty because C# fills each slot with the type's default value.

    For example, if you create an array of six numbers but don't specify the numbers, you'll end up with an array containing six 0s because 0 is the default value for numbers in C#.

    var numbers = new int[6]; // <- An "empty" array.
    
    foreach (var number in numbers)
      Debug.Log(number);
    
    // Output:
    
    // 0
    // 0
    // 0
    // 0
    // 0
    // 0

    Accessing Values in Arrays

    Arrays in C# are zero-indexed; you count their items starting from 0. For example, in an array with five items, the first item has an index of 0, the second has an index of 1, and so on.

    You can access the items using an indexer. An indexer is a pair of square brackets around an item's index at the end of the array's name.

    string[] agents = { "Brimstone", "Reyna", "Phoenix" };
    
    Debug.Log(agents[0]); // Logs 'Brimstone'.
    Debug.Log(agents[1]); // Logs 'Reyna'.
    Debug.Log(agents[2]); // Logs 'Phoenix'.
    
    ❌ // Trying to access 4th item in a list of 3.
    Debug.Log(agents[3]);

    Tip

    If you try accessing an item whose index is higher than the last array item or lower than the first item, you'll get an IndexOutOfRange error.

    Array Methods and Properties

    Each array has a Length property you can access using dot notation that returns the total items it contains. It's useful when looping through the array's items using a for loop.

    With the array's total as the condition, the loop executes the same number of times as the total items.

    string[] legends = { "Wraith", "Seer", "Octane" };
    
    // 'Length' property determines how many times loop executes.
    for (var i = 0; i < legends.Length; i++)
      Debug.Log(legends[i]);
    
    // Output:
    
    // Wraith
    // Seer
    // Octane

    Tip

    A for loop is a good choice when looping through lists and arrays if you need to know each item's index.

    Arrays have some helpful built-in methods you can call while making your game in the Unity Game Engine:

    • Sort - sorts items in the array in ascending order. Works well with numbers.
    • Reverse - reverses the order of items in an array.
    • Clear - resets all items in the array to their default values, essentially "clearing" it.
    • IndexOf - returns the index of the specified item in an array.
    • Find - searches for the first item in an array that meets a specific criterion.

    How to Use Lists in Unity

    Lists in C# are collection types that store a flexible number of items. Unlike arrays, you can add and remove items from a list.

    The System.Collections.Generic namespace stores lists in C#, so you'll have to add the using statement at the top of the file if you want to use them in your Unity game's scripts.

    // This line is mandatory when using lists.
    using System.Collections.Generic;
    
    List<string> elements = ["Fire", "Ice", "Water"];
    
    var numbers = new List<float>() { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
    
    var weapons = new List<Weapon>(); // <- An empty list.

    Tip

    A namespace stores many related classes, and using statements grant access to them if you're working in a separate namespace.

    You declare a list's item type between angled brackets (the greater-than and less-than symbols) because lists are generic.

    Generics are a broad topic, but simply put, they provide flexibility when working with C# types by letting the game developer decide which type to use.

    If you see any type surrounded by angled brackets, there's a good chance that code is using generics.

    You can create a list instance using the new keyword and store it in a variable, just like arrays.

    Lists are genuinely empty by default, and you can't specify the initial number of items because the list grows and shrinks automatically as items are added and removed.

    Accessing Values in Lists

    Lists in C# are also zero-indexed, and their items are accessible using an indexer, like in arrays.

    using System.Collections.Generic;
    
    List<string> craftingMaterials =
    [
      "Bandages",
      "Wire",
      "Duct Tape"
    ];
    
    Debug.Log(craftingMaterials[0]); // Logs 'Bandages'.

    List Methods and Properties

    Each list has a Count property that returns the total number of items in the list and is accessed using dot notation.

    You can also loop through a list using the for or foreach loops.

    For example, if the player in your Unity game collects ammo from a crate that replenishes the ammo of all weapons, you can loop through the weapons list and update each weapon's ammo value to its maximum.

    using System.Collections.Generic;
    
    var player = new Player();
    
    const byte MaxAmmo = 200;
    
    // Assume the player opened a large ammo crate...
    
    foreach(var weapon in player.Weapons)
      weapon.ammo = MaxAmmo; // <- Replenish each weapon's ammo.

    Some of the built-in list methods you might find helpful when making a game include:

    • Add - adds a specified item at the end of the list.
    • Remove - removes a specified item from the list.
    • Clear - removes all items from the list.
    • Contains - checks if a list contains a specified item and returns true if it does, or false if it doesn't.
    • Reverse - reverses the order of items in the list.

    Lists vs. Arrays: Which Should You Use?

    Despite the similarities between lists and arrays, there are situations where one works better than the other.

    Use an array if:

    • You're working with a fixed number of items.
    • You know how many items you'll be working with.
    • You can keep track of the "empty" slots (items with default values) when updating arrays.

    Use a list if:

    • You're working with a dynamic number of items.
    • You plan to add and remove items often.
    • You prefer dealing with a genuinely empty collection instead of one that still has values when "empty".

    Recap

    • Lists and arrays are collection types that store related items.
    • Arrays store a fixed number of items.
    • Lists store a dynamic number of items.
    • Both lists and arrays are zero-indexed, meaning you count each item's position starting from 0.
    • You can use loops to manipulate items in lists and arrays.

    This article is the fourteenth in a series breaking down common C# concepts used in game development with the Unity Game Engine.

    The next one discusses how to use dates and times in your game.

    Here's the complete list of articles in the series:

    • A Game Developer's Guide to Variables in C#
    • A Game Developer's Guide to Constants in C#
    • A Game Developer's Guide to Numbers in C#
    • A Game Developer's Guide to Operators in C#
    • A Game Developer's Guide to Strings in C#
    • A Game Developer's Guide to Booleans in C#
    • A Game Developer's Guide to Enums in C#
    • A Game Developer's Guide to Conditional Statements in C#
    • A Game Developer's Guide to Loops in C#
    • A Game Developer's Guide to Classes in C#
    • A Game Developer's Guide to Access Modifiers in C#
    • A Game Developer's Guide to Fields & Properties in C#
    • A Game Developer's Guide to Methods in C#
    • A Game Developer's Guide to Lists & Arrays in C# (you are here)
    • A Game Developer's Guide to Dates & Times in C#

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon

    Latest

    • A Game Developer's Guide to Dates and Times in C#December 12, 2025
    • A Game Developer's Guide to Lists and Arrays in C#December 12, 2025
    • A Game Developer's Guide to Methods in C#December 12, 2025

    Suggested

    • constants headerA Game Developer's Guide to Constants in C#December 12, 2025
    • A reflection probe in front of a house in Unity.Reflection Probes in Unity: How to Get the Most Out of ThemNovember 2, 2025
    • How to Bake Lights in Unity: A Step-by-Step GuideNovember 2, 2025
    • strings header imageA Game Developer's Guide to Strings in C#December 12, 2025
    • A Game Developer's Guide to Loops in C#December 12, 2025

    Become a Patreon member and receive:

    • 30+ articles ad-free.

    • 100+ exclusive videos on C# and how to use Unity.

    • 40+ cheat sheets on all things game development.

    • 2 Custom Visual Studio Themes.

    • 2-day early access to articles and YouTube videos.

    Join Patreon