Codectionary / Developer documentation / C#

Arrays

An array in C# is a fixed-size, ordered collection of elements of the same type, zero-indexed like most C# collections. Once created, an array's length cannot change - use a List<T> instead if you need a resizable collection. C# supports single-dimensional arrays, multi-dimensional arrays (a true grid, declared with a comma like int[,]), and jagged arrays (an array of arrays, where each inner array can have a different length).

Read the syntax

Type[] declares a one-dimensional array of fixed length. Indexes start at 0 and must be less than Length; accessing outside that range throws an IndexOutOfRangeException.

A rectangular array uses commas inside brackets and a fixed grid shape; a jagged array is an array of arrays whose rows may differ in length. Array.Sort and Array.Reverse change the array in place.

Syntax

type[] arrayName = new type[size];

Examples

Creating and Accessing Arrays

Declaring arrays with initial values and reading elements by index.

int[] numbers = { 10, 20, 30, 40, 50 };
string[] names = new string[3];
names[0] = "John";
names[1] = "Zain";
names[2] = "Jamal";

Console.WriteLine(numbers[0]);    // 10
Console.WriteLine(numbers.Length); // 5
Console.WriteLine(names[1]);       // Zain
  1. Declaring arrays with initial values and reading elements by index.
  2. Type[] declares a one-dimensional array of fixed length. Indexes start at 0 and must be less than Length; accessing outside that range throws an IndexOutOfRangeException.
  3. numbers contains five values, so numbers[0] prints 10 and Length prints 5. Assignments fill names at indexes 0 through 2; names[1] prints Zain.

Iterating Over an Array

Looping through array elements with a standard for loop or a foreach loop.

int[] scores = { 85, 92, 78, 90 };

for (int i = 0; i < scores.Length; i++)
{
    Console.WriteLine($"Index {i}: {scores[i]}");
}

// foreach - simpler when you don't need the index
foreach (int score in scores)
{
    Console.WriteLine(score);
}
  1. Looping through array elements with a standard for loop or a foreach loop.
  2. Type[] declares a one-dimensional array of fixed length. Indexes start at 0 and must be less than Length; accessing outside that range throws an IndexOutOfRangeException.
  3. The for loop prints each index beside its score and stops before Length to avoid an invalid index. foreach prints the same values without tracking indexes.

Multi-Dimensional and Jagged Arrays

A true 2D grid versus an array of arrays with potentially different lengths.

int[,] grid = {
    { 1, 2, 3 },
    { 4, 5, 6 }
};
Console.WriteLine(grid[1, 2]);  // 6 (row 1, column 2)

int[][] jagged = new int[2][];
jagged[0] = new int[] { 1, 2, 3 };
jagged[1] = new int[] { 4, 5 };  // different length - that's allowed
Console.WriteLine(jagged[1][1]);  // 5
  1. A true 2D grid versus an array of arrays with potentially different lengths.
  2. Type[] declares a one-dimensional array of fixed length. Indexes start at 0 and must be less than Length; accessing outside that range throws an IndexOutOfRangeException.
  3. grid[1, 2] reads the second row and third column, which is 6. jagged[1][1] reads the second value in the shorter second row, which is 5.

Common Array Operations with System.Array

Using the built-in Array class for sorting, reversing, and searching.

int[] numbers = { 5, 2, 8, 1, 9 };

Array.Sort(numbers);
Console.WriteLine(string.Join(", ", numbers));  // 1, 2, 5, 8, 9

Array.Reverse(numbers);
Console.WriteLine(string.Join(", ", numbers));  // 9, 8, 5, 2, 1

int index = Array.IndexOf(numbers, 5);
Console.WriteLine(index);  // 2
  1. Using the built-in Array class for sorting, reversing, and searching.
  2. Type[] declares a one-dimensional array of fixed length. Indexes start at 0 and must be less than Length; accessing outside that range throws an IndexOutOfRangeException.
  3. Sort changes the original array into ascending order and Reverse changes that order to descending. IndexOf then finds 5 at index 2 in the modified array.

Best practices

  • Use a List<T> instead of a plain array whenever the collection needs to grow or shrink dynamically
  • Use foreach when you just need the values, and a standard indexed for loop when you need the index too
  • Use string.Join() to print array contents readably - printing an array directly with WriteLine() shows its type name, not its contents
  • Prefer jagged arrays (int[][]) over true multi-dimensional arrays (int[,]) when rows might have different lengths - jagged arrays are also generally faster in C#

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading