In C#, List.Slice(start, length) returns a new list that holds a copy of a block of items. It works like GetRange(): the first number is where to start and the second is how many items to take.
The main reason it exists is the range syntax from C# 8. When a type has Count and a Slice method, the compiler lets you write list[1..4] (items at indexes 1, 2, and 3) and turns it into a Slice call for you. You can also count from the end with ^, as in list[^2..], which means "the last two items".
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Slice(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length. List<T>.Slice is only in newer .NET (.NET Core 3.0 and later, and .NET 5 and later). It is not in .NET Framework. Using list[1..4] also needs C# 8 or later, so check your project version.
Syntax
Call Slice directly, or use the range operator and let the compiler call it.
C#
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
List<int> a = numbers.Slice(1, 3); // 20, 30, 40
List<int> b = numbers[1..4]; // same, via range syntax
List<int> c = numbers[^2..]; // 40, 50
Call
What it does
numbers.Slice(1, 3)
3 items starting at index 1. Same as GetRange(1, 3).
numbers[1..4]
Range syntax: from index 1 up to, but not including, index 4.
numbers[..2]
The first two items. Leaving out the start means index 0.
With the range operator, the second number is an end index that is not included, while Slice takes a length. 1..4 means Slice(1, 3). The result is always a newList<T> (a shallow copy), unlike a span slice, which is a view. An invalid range throws an exception, just like GetRange.
Library declarations
C#
public List<T> Slice(int start, int length);
Parameters
Parameter
Description
start
Required. The zero-based index where the block begins.
length
Required. How many items to copy. start + length must not be more than Count.
Return Value
Return Value
Description
List
A new list with a shallow copy of the block. The source list is unchanged.
C# Examples
Example 1 — Slice a List by Start and Length
Take three items from index 1 and compare with GetRange.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
List<int> a = numbers.Slice(1, 3);
List<int> b = numbers.GetRange(1, 3);
Console.WriteLine(string.Join(", ", a));
Console.WriteLine(string.Join(", ", b));
a.Add(99);
Console.WriteLine(numbers.Count);
}
}
Output
20, 30, 40
20, 30, 40
5
How It Works
Same arguments. Both calls take a start index and a number of items, so (1, 3) selects indexes 1, 2, and 3.
Same result. The two printed lines are identical. Slice gives you what GetRange gives you.
A copy. Adding 99 to a grows only a. It never touches numbers.
Original size.numbers.Count is still 5.
Example 2 — Use the Range Operator
Write list[start..end] and let the compiler call Slice for you. Also count from the end with ^.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
List<int> middle = numbers[1..4];
List<int> head = numbers[..2];
List<int> tail = numbers[^2..];
Console.WriteLine(string.Join(", ", middle));
Console.WriteLine(string.Join(", ", head));
Console.WriteLine(string.Join(", ", tail));
Console.WriteLine(numbers[^1]);
}
}
Output
20, 30, 40
10, 20
40, 50
50
How It Works
End is not included.1..4 starts at index 1 and stops before index 4, so it covers indexes 1, 2, and 3. The compiler turns it into Slice(1, 3).
Open start...2 means "from the beginning up to, not including, index 2", so it gives 10, 20.
From the end.^2 means "two places from the end", which is index 3. So [^2..] takes everything from there: 40, 50.
Single item.numbers[^1] reads the last item with the same ^ idea. It is the same as numbers[numbers.Count - 1].
Example 3 — A Range Outside the List
Ask for more than the list has. The call fails, and the slice from a valid range is still a separate copy.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> days = new List<string> { "Mon", "Tue", "Wed" };
try {
List<string> bad = days.Slice(1, 5);
Console.WriteLine(bad.Count);
} catch (ArgumentException) {
Console.WriteLine("range is too long");
}
List<string> copy = days[0..3];
copy[0] = "Sun";
Console.WriteLine(days[0] + " " + copy[0]);
}
}
Output
range is too long
Mon Sun
How It Works
Too long. From index 1 only two items exist, but the call asks for five. The list reports the problem with an exception.
Catch the family. The error is an ArgumentException or its child ArgumentOutOfRangeException. Catching ArgumentException handles both.
Whole list copy.days[0..3] selects all three items, so it is a full copy of the list.
Independent copy. Changing copy[0] to "Sun" leaves days[0] as "Mon", which shows the slice is not a live view.
Frequently Asked Questions
It returns a new List<T> with a copy of length items that begin at start. It is the method the compiler calls for range syntax such as list[1..4].
List<T>.Slice is available in newer .NET (.NET Core 3.0 and later, and .NET 5 and later), not in .NET Framework. The list[a..b] syntax needs C# 8 or later. Check the Microsoft docs for your exact target.
They produce the same result. Slice exists to support the range operator. GetRange has been available in all versions of .NET.
A copy. The new list is separate, so adding to or removing from it does not change the original. The items are shallow copies.
It counts from the end. ^1 is the last item, ^2 is the second from the end. So list[^2..] takes the last two items.