Skip to content
Tutorials Center
My learning

Topics

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.

C#
int[] primes = { 2, 3, 5, 7 };
int[,] grid = new int[3, 4];

Try it

C# Create, index and slice

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.

C# Array helpers and a 2D array

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

C#
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.Copy or a range (a[..]) to copy.
  • Ranges copy. b[1..4] creates a new array; use AsSpan(1, 3) for a view with no copy.
  • Fixed size. Array.Resize creates a new array and copies. Adding in a loop is O(nยฒ); use List<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; foreach over an array is as fast as for.
  • Equality. == on arrays compares references; use SequenceEqual for content.
  • ToString() prints the type name (System.Int32[]), not the elements. Use string.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.