Arrays and multidimensional data
↑Examples are independent C# snippets for modern .NET console projects unless noted otherwise. Class declarations and top-level code can live in Program.cs when top-level statements come first.
At a glance
Section titled “At a glance”An array stores a fixed number of elements of one element type. It gives constant-time access by index and is a fundamental building block for algorithms.
How it works
Section titled “How it works”Create, access and update
Section titled “Create, access and update”Array indexes start at zero. Length is the number of elements. Arrays have fixed length, although the elements can normally be replaced.
int[] scores = { 7, 3, 9 };scores[1] = 5;Console.WriteLine(scores[0]); // 7Console.WriteLine(scores.Length); // 3Loop and sort
Section titled “Loop and sort”Use a for loop if you need both an index and a value, or foreach if you only need the values. Array.Sort modifies the array in place, so copy it first if the original order matters.
int[] original = { 38, 12, 27 };int[] sorted = (int[])original.Clone();Array.Sort(sorted);foreach (int number in sorted) Console.WriteLine(number); // 12, 27, 38Rectangular and jagged arrays
Section titled “Rectangular and jagged arrays”A rectangular array (int[,]) has a fixed grid shape. A jagged array (int[][]) is an array of arrays, so each row can have a different length.
int[,] grid = { { 1, 2 }, { 3, 4 } };Console.WriteLine(grid[1, 0]); // 3int[][] rows = { new[] { 1 }, new[] { 2, 3 } };Console.WriteLine(rows[1][1]); // 3Deep dive: index arithmetic, copying, and choosing collections
Section titled “Deep dive: index arithmetic, copying, and choosing collections”An array has a fixed length, not fixed values
Section titled “An array has a fixed length, not fixed values”new int[3] allocates three integer slots initialized to zero. You can replace their values, but you cannot resize the same array object. Indexes run from 0 to Length - 1. The expression numbers[^1] refers to the last element, while a range such as numbers[1..3] creates a new array containing the selected slice for ordinary arrays.
Copy an array when mutations must be isolated
Section titled “Copy an array when mutations must be isolated”An array is a reference type. Assigning int[] second = first; creates another reference to the same array. Changing second[0] changes what first[0] reads. Array.Clone() creates a shallow copy: for arrays of integers that gives separate values, but for arrays of class instances the elements still refer to the same objects.
int[] original = { 38, 12, 27 };int[] copy = (int[])original.Clone();Array.Sort(copy);Console.WriteLine(string.Join(", ", original)); // 38, 12, 27Console.WriteLine(string.Join(", ", copy)); // 12, 27, 38Arrays, lists, and spans solve different needs
Section titled “Arrays, lists, and spans solve different needs”An array is useful for fixed-size indexed storage. List<T> grows when elements are added and exposes methods like Add and Remove; internally it manages storage capacity, which may be larger than its current Count. A Span<T> provides a bounded view over contiguous memory without necessarily making a new array, but it has usage and lifetime restrictions. Start with arrays or List<T> and reach for spans when a measured performance need justifies them.
Multidimensional versus jagged storage
Section titled “Multidimensional versus jagged storage”int[,] grid describes a rectangular array with dimensions accessible through GetLength(dimension). int[][] rows is an array whose elements are arrays, so the rows can have different sizes or even be null. Choose based on your data rather than assuming one representation is automatically faster.
Complexity mental model
Section titled “Complexity mental model”Indexing an array is normally O(1). Searching an unsorted array is usually O(n) in the worst case; sorting costs more and changes order. A binary search requires the data to already be sorted in the expected comparison order. Readability and correctness are more valuable than a theoretical improvement when data sets are tiny.
When and why
Section titled “When and why”Use arrays for fixed-size ordered values and when tight indexed access is helpful; consider List<T> when size changes frequently.
Common mistake
Section titled “Common mistake”array[array.Length] is out of range. Array.Sort mutates the array; it does not return a new sorted array.
Try it yourself
Section titled “Try it yourself”Sort a copy of an array without changing the original.
Reveal a solution
int[] copy = (int[])numbers.Clone(); Array.Sort(copy);.
