Selecting a range of items inside an array in C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
C# provides several ways to select a range of items from an array. Starting with C# 8.0, the range operator .. and Index type (^ for end-relative indexing) allow concise slicing syntax like array[1..4]. For earlier versions, Array.Copy(), ArraySegment<T>, LINQ's Skip().Take(), and Span<T> provide range selection. The range operator is the most readable approach for modern C# code.
Range Operator (C# 8.0+)
The .. operator creates a Range value. The start index is inclusive, the end index is exclusive — matching the convention used by Python slicing and most languages.
Index from End (^)
^n means "n positions from the end". ^0 is past the end (same as numbers.Length), so numbers[^3..^0] is equivalent to numbers[^3..].
ArraySegment<T> (Zero-Copy View)
ArraySegment<T> wraps a portion of an array without copying. Changes to the segment modify the original array.
Span<T> (High-Performance Slicing)
Span<T> is stack-allocated and provides the highest performance for array slicing. It cannot be stored in fields or used across await boundaries.
Array.Copy
Array.Copy is available in all .NET versions and creates an independent copy.
LINQ Skip and Take
LINQ is the most flexible but slowest option due to iterator overhead and allocation.
Comparison Table
| Method | Copies Data | Min C# Version | Performance | Syntax |
array[1..4] | Yes | 8.0 | Good | array[start..end] |
Span<T> | No | 7.2 | Best | array.AsSpan(offset, length) |
ArraySegment<T> | No | 2.0 | Good | new ArraySegment(array, offset, count) |
Array.Copy | Yes | 1.0 | Good | Array.Copy(src, srcIdx, dst, dstIdx, len) |
LINQ Skip/Take | Yes | 3.5 | Slow | array.Skip(n).Take(m).ToArray() |
Common Pitfalls
- Range end index is exclusive:
numbers[1..4]returns elements at indices 1, 2, 3 — not 1, 2, 3, 4. This matches Python and most other languages but can surprise developers expecting an inclusive range. - Range operator creates a copy:
numbers[1..4]allocates a new array. For performance-critical code, useSpan<T>which provides a zero-copy view. The range operator onSpan<T>does not allocate. - Span cannot be used across await:
Span<T>is a ref struct and cannot be stored in async method state machines. UseMemory<T>orArraySegment<T>when you need to pass array slices acrossawaitboundaries. ArraySegmentmodifications affect the original array: SinceArraySegmentis a view, writing to it changes the source array. If you need an independent copy, use the range operator orArray.Copy.- Index out of range with
^n:numbers[^0]throwsIndexOutOfRangeExceptionbecause^0equalsnumbers.Length. The last valid index is^1. Similarly,numbers[^8..]on a 7-element array throws because the start index is before the array.
Summary
- Use
array[1..4](C# 8.0+) for the most readable range selection - Use
^nfor end-relative indexing —^1is the last element,^2is second to last - Use
Span<T>for zero-copy, high-performance slicing - Use
ArraySegment<T>when you need a view that works acrossawaitboundaries - Use
Array.Copyfor an independent copy in older .NET versions - Range end indices are exclusive —
[1..4]returns elements at indices 1, 2, 3
Related reading
- Selection algorithms on sorted matrix
- SELinux is not supported with the overlay graph driver
- Send byte array to storm kafka bolt
- Separate the alphabet and digit such that their relative order remains the same in On time and O1 space
- Selecting a row in DataGridView programmatically
- Send email from Elmah?
- Separating celery consumer and producer
- Serialize Class containing Dictionary member

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.