What is the difference between a list and an array in C#?

An array stores a fixed-size sequential collection of elements of the same type, whereas a list is a generic collection that can dynamically resize during runtime. Understanding the differences between arrays and lists is crucial for choosing the right data structure for your C# applications.

Syntax

Following is the syntax for declaring and initializing an array −

dataType[] arrayName = new dataType[size];
dataType[] arrayName = {value1, value2, value3};

Following is the syntax for declaring and initializing a list −

List<dataType> listName = new List<dataType>();
List<dataType> listName = new List<dataType> {value1, value2, value3};

Key Differences

Feature Array List
Size Fixed size, defined at creation Dynamic size, can grow/shrink
Performance Faster access and memory efficient Slightly slower due to dynamic resizing
Methods Limited built-in methods Rich set of methods (Add, Remove, Contains, etc.)
Memory Allocated on stack or heap Always allocated on heap

Using Arrays

Example

using System;

class Program {
   public static void Main() {
      // Declare and initialize array
      int[] numbers = new int[5] {23, 14, 11, 78, 56};
      
      Console.WriteLine("Array elements:");
      for (int i = 0; i < numbers.Length; i++) {
         Console.WriteLine("Index " + i + ": " + numbers[i]);
      }
      
      // Modify an element
      numbers[2] = 99;
      Console.WriteLine("After modification: " + numbers[2]);
   }
}

The output of the above code is −

Array elements:
Index 0: 23
Index 1: 14
Index 2: 11
Index 3: 78
Index 4: 56
After modification: 99

Using Lists

Example

using System;
using System.Collections.Generic;

class Program {
   public static void Main() {
      // Declare and initialize list
      List<string> cars = new List<string>();
      
      // Add elements
      cars.Add("Audi");
      cars.Add("BMW");
      cars.Add("Chevrolet");
      cars.Add("Hyundai");
      
      Console.WriteLine("List elements:");
      for (int i = 0; i < cars.Count; i++) {
         Console.WriteLine("Index " + i + ": " + cars[i]);
      }
      
      // Remove an element
      cars.Remove("BMW");
      Console.WriteLine("After removing BMW, count: " + cars.Count);
      
      // Check if element exists
      bool hasAudi = cars.Contains("Audi");
      Console.WriteLine("Contains Audi: " + hasAudi);
   }
}

The output of the above code is −

List elements:
Index 0: Audi
Index 1: BMW
Index 2: Chevrolet
Index 3: Hyundai
After removing BMW, count: 3
Contains Audi: True

Performance Comparison

Example

using System;
using System.Collections.Generic;
using System.Diagnostics;

class Program {
   public static void Main() {
      const int size = 1000000;
      Stopwatch sw = new Stopwatch();
      
      // Array performance test
      sw.Start();
      int[] array = new int[size];
      for (int i = 0; i < size; i++) {
         array[i] = i;
      }
      sw.Stop();
      Console.WriteLine("Array creation time: " + sw.ElapsedMilliseconds + " ms");
      
      // List performance test
      sw.Restart();
      List<int> list = new List<int>();
      for (int i = 0; i < size; i++) {
         list.Add(i);
      }
      sw.Stop();
      Console.WriteLine("List creation time: " + sw.ElapsedMilliseconds + " ms");
   }
}

The output will show that arrays are generally faster for fixed-size operations −

Array creation time: 2 ms
List creation time: 15 ms

When to Use Arrays vs Lists

Use Arrays when:

  • You know the exact size of your collection beforehand

  • Performance and memory efficiency are critical

  • You need multi-dimensional data structures

Use Lists when:

  • The size of your collection may change during runtime

  • You need built-in methods like Add, Remove, Contains

  • Flexibility is more important than slight performance differences

Conclusion

Arrays provide better performance and memory efficiency for fixed-size collections, while Lists offer flexibility and rich functionality for dynamic collections. Choose arrays when size is known and performance is critical, and choose lists when you need dynamic resizing and convenient methods.

Updated on: 2026-03-17T07:04:35+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements