Ranges and jagged arrays
Index and range syntax makes selections from a sequence readable. The caret counts from the end, while a range specifies a start and an exclusive end. Jagged arrays use nested indexing to access rows that can have different lengths.
using System;int[] counts = [3, 5, 7, 9];int[] middle = counts[1..3];Console.WriteLine(counts[^1]);Console.WriteLine(string.Join(", ", middle));middle[0] = 100;Console.WriteLine(counts[1]);The end is exclusive and changing the separate slice does not change the source.
Expected output (reasoned, not executed):
95, 75Read positions and half-open intervals
For a nonempty array, values[^1] is the last item. ^0 denotes the position immediately after the final item and is not a valid single-element index. values[1..3] selects indices 1 and 2, not index 3. Omit an endpoint to select from the start or to the end. On a zero-based sequence of length n, the resolved range endpoints must satisfy 0 <= start <= end <= n.
Understand the result type
Slicing an int array using a range creates a separate array for a nonempty slice; editing its integer slots does not change the source slots. This is not a general rule for every range-capable type: Span<T> can represent a view, a later topic. On .NET 8 and later, List<T> has a Slice(start, length) method, so C# can use range syntax via its Count, indexer and Slice members. For example, items[1..3] returns a shallow sublist with two elements. GetRange(start, count) is an explicit alternative, including for older targets without Slice. Neither copy clones referenced objects. Index and range syntax was introduced in C# 8.
Use the correct shape for two-dimensional data
int[][] is a jagged array, with each element referring to a separate array that can have its own length. int[,] is a rectangular two-dimensional array with a fixed size along each axis. Use grid.GetLength(0) and grid.GetLength(1) for rectangular dimensions. When constructing a jagged outer array without initial rows, its row references start as null; initialize or check them before reading nested indices.
using System;int[][] shelves = [[2, 4], [8]];Console.WriteLine(shelves[0].Length);Console.WriteLine(shelves[1].Length);Console.WriteLine(shelves[1][0]);Each row has its own bounds. The second row only has index zero.
Expected output (reasoned, not executed):
218using System;int[,] grid = { { 1, 2, 3 }, { 4, 5, 6 } };Console.WriteLine(grid.GetLength(0));Console.WriteLine(grid.GetLength(1));Console.WriteLine(grid.Length);Console.WriteLine(grid[1, 2]);GetLength reports per-axis sizes; Length is the total number of elements.
Expected output (reasoned, not executed):
2366Common mistake and correction
Do not use values[^0] to read the last element; use ^1. A range ending at ^0 is valid because the end is exclusive. Do not assume that shelves[0].Length applies to every row. Use each row’s actual length and handle missing rows when they are possible.
Try it yourself
For [10, 20, 30, 40, 50], select the last two elements using a range, then set the first selected element to 99. Report the source and slice. For jagged rows [[1, 2], [], [3]], compute a total using nested foreach loops.
Next: Methods and return values. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Ranges and jagged arrays — knowledge check
Apply the lesson concepts to the following scenarios.
What is a[^1] for a = [4, 8, 12]?
What does a[1..3] select from [3, 5, 7, 9]?
Can ^0 be a valid exclusive end in an array range?
What is the initial value of a row in new int[2][]?
What is grid.GetLength(1) for a rectangular int[2,3]?