Readable C# equivalent of Python slice operation
If you have a List
GetRange can come in handy.
From MSDN link:
A shallow copy of a collection of reference types, or a subset of that collection, contains only the references to the elements of the collection. The objects themselves are not copied. The references in the new list point to the same objects as the references in the original list.
The Slice
function can then be:
public static IEnumerable<T> Slice<T>(this List<T> source, int from, int to) => source.GetRange(from, to - from);
Negative ranges that python slice supports can also be handled with some loss of cleanliness.