C# Reference ยท Types and Literals
C# arrays
Reference for C# arrays: declaring, initializing, indexing, ranges, the System.Array methods, multidimensional arrays, covariance and common mistakes.
An array is a fixed-size, zero-based sequence of elements of one type, stored contiguously in memory. Arrays are reference types that derive from System.Array; the elements live on the heap even when they are ints. The length is set when the array is created and never changes. For a list that grows, use List<T>.
C# has single-dimensional arrays (int[]), rectangular multidimensional arrays (int[,]) and arrays of arrays (int[][], see jagged arrays). A single-dimensional array of a value type is the fastest collection in .NET for indexing and scanning.
int[] primes = { 2, 3, 5, 7 }; int[,] grid = new int[3, 4];
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
T[] name = new T[length]; T[] name = new T[] { a, b, c }; T[] name = { a, b, c }; // only in a declaration T[] name = [a, b, c]; // collection expression (C# 12) T[,] grid = new T[rows, cols]; T[,,] cube = new T[x, y, z];
| Member | Description |
|---|---|
Length, LongLength, Rank |
Total element count, as long, and number of dimensions. |
GetLength(dim), GetLowerBound, GetUpperBound |
Size and bounds of one dimension. |
Array.Sort, Reverse, BinarySearch, IndexOf, Exists, Find, FindAll |
Static helpers; BinarySearch needs a sorted array. |
Array.Copy, CopyTo, Clone, Resize, Fill, Clear |
Copy and modify. Clone is shallow. |
AsSpan(), AsMemory() |
Zero-allocation views. |
Default values. A new array is filled with default(T): 0, false or null.
Errors. An index outside the bounds throws IndexOutOfRangeException. A negative length throws OverflowException. Storing an object of the wrong runtime type in a covariant array throws ArrayTypeMismatchException.
Usage notes
- Reference semantics. Assigning an array to another variable shares one array; use
Clone,Array.Copyor a range (a[..]) to copy. - Ranges copy.
b[1..4]creates a new array; useAsSpan(1, 3)for a view with no copy. - Fixed size.
Array.Resizecreates a new array and copies. Adding in a loop is O(nยฒ); useList<T>. - Covariance is unsafe.
object[] o = new string[1];compiles, but writing a non-string throws at run time. This is a historical design that generic collections avoid. - Bounds checks. The JIT removes many checks in
for (i = 0; i < a.Length; i++)loops;foreachover an array is as fast asfor. - Equality.
==on arrays compares references; useSequenceEqualfor content. ToString()prints the type name (System.Int32[]), not the elements. Usestring.Join.- Large arrays over about 85,000 bytes go on the Large Object Heap and are collected less often.
- Multidimensional arrays are slower than jagged arrays in .NET because indexing uses a helper.