This post will discuss how to get a slice or a sublist of a list in C#.

1. Using GetRange() Method

The recommended solution to get a copy of the source list between the specified range is using the List<T>.GetRange() method. The following example demonstrates how you can use the GetRange() method to get a sublist of a list.

Download  Run Code

 
If you need to delete the range of items from the source list, use the List<T>.RemoveAt(Int32) method that takes the index of the List<T> and remove the element at that index.

Download  Run Code

2. Using Where() Method

With LINQ, you can use the Where() method to filters a sequence of values based on a predicate. The following code example demonstrates how you can use the Where() method to get a slice of a list in C#.

Download  Run Code

That’s all about getting a slice or a sublist of a list in C#.