0
0
CsharpComparisonBeginner · 4 min read

List vs Array in C#: Key Differences and When to Use Each

In C#, a List<T> is a flexible, resizable collection that allows easy addition and removal of items, while an Array has a fixed size and better performance for fixed-size data. Use List<T> when you need dynamic sizing and Array when you know the size in advance and want faster access.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of List<T> and Array in C# based on key factors.

FactorList<T>Array
SizeDynamic, can grow or shrinkFixed, size set at creation
PerformanceSlightly slower due to resizing and overheadFaster for fixed size and direct access
FlexibilitySupports adding/removing elements easilyNo adding/removing after creation
MemoryUses more memory due to overheadMore memory efficient
UsageBest for collections with unknown or changing sizeBest for fixed-size collections
Type SafetyGeneric and type-safeType-safe but fixed type
⚖️

Key Differences

Array in C# is a fixed-size collection that you must define with a specific length when you create it. Once created, you cannot change its size. This makes arrays very fast and memory efficient because the system knows exactly how much space to allocate. Arrays are great when you know the exact number of elements beforehand.

On the other hand, List<T> is a generic collection that can grow or shrink dynamically. It manages an internal array and resizes it automatically when you add or remove items. This flexibility comes with a small performance cost and slightly more memory usage. Lists provide many useful methods like Add, Remove, and Insert that make managing collections easier.

In summary, use Array for fixed-size, performance-critical scenarios, and use List<T> when you need a flexible, easy-to-use collection that can change size during runtime.

⚖️

Code Comparison

This example shows how to create a collection of numbers, add an item, and print all items using an Array in C#.

csharp
using System;

class Program
{
    static void Main()
    {
        int[] numbers = new int[3] { 1, 2, 3 };
        // Arrays have fixed size, so we cannot add directly
        // To add, we create a new array with larger size
        int[] newNumbers = new int[numbers.Length + 1];
        for (int i = 0; i < numbers.Length; i++)
        {
            newNumbers[i] = numbers[i];
        }
        newNumbers[newNumbers.Length - 1] = 4;
        numbers = newNumbers;

        foreach (int num in numbers)
        {
            Console.Write(num + " ");
        }
    }
}
Output
1 2 3 4
↔️

List<T> Equivalent

This example shows the same task using List<int>, which is simpler and more flexible.

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3 };
        numbers.Add(4); // Easy to add new item

        foreach (int num in numbers)
        {
            Console.Write(num + " ");
        }
    }
}
Output
1 2 3 4
🎯

When to Use Which

Choose Array when you know the exact number of elements in advance and want the best performance with minimal memory overhead. Arrays are ideal for fixed-size data like storing coordinates or fixed-length buffers.

Choose List<T> when you need a collection that can grow or shrink dynamically, or when you want easy methods to add, remove, or search items. Lists are perfect for most general-purpose programming where collection size changes over time.

Key Takeaways

Use Array for fixed-size, performance-critical collections.
Use List<T> for flexible, resizable collections with easy management.
Arrays have better memory efficiency but no resizing after creation.
Lists provide useful methods and dynamic sizing at a small performance cost.
Choose based on whether collection size is fixed or dynamic in your program.