Slice - Kalkwst/Risotto GitHub Wiki
Extracts a portion of the source sequence into a new sequence selected from start to end (including end), where start and end represent the index of items in that sequence.
In this page
| Overload | Description |
|---|---|
| Slice(int start, int end) | Extract a portion of the source sequence into a new sequence from start to end. |
Extracts a portion of the source sequence into a new sequence selected from start to end (including end), where start and end represent the index of items in that sequence.
public static IEnumerable<TSource> Slice<TSource>(this IEnumerable<TSource> source, int start, int end);TSource
The type of the elements of the original sequence
source IEnumerable <TSource>
The source sequence.
start Int32
A zero-based index at which to start the extraction.
end Int32
A zero-based index at which to end the extraction.
IEnumerable<TResult>
A new enumerable containing the extracted elements.
ArgumentNullException
source is null
The following code examples demonstrate the use of Slice(int start, int end) with various start and end options.
string[] animals = new string[] { "ant", "bison", "camel", "duck", "elephant" };
animals.Slice(2, 4);
//=> [ "camel", "duck", "elephant" ]
animals.Slice(2, 3);
//=> [ "camel", "duck" ]
animals.Slice(1, 4);
//=> [ "bison", "camel", "duck", "elephant" ]
animals.Slice(-2, 3);
//=> [ "duck", "elephant" ]
animals.Slice(2, -1);
//=> [ "camel", "duck", "elephant" ]
animals.Slice(0, 30);
//=> [ "ant", "bison", "camel", "duck", "elephant" ]A negative start index can be used, indicating an offset from the end of the sequence. If start is greater than the index range of the sequence, an empty array is returned.
A negative end index can be used, indicating an offset from the end of the sequence. If end is greater than the length of the sequence, slice extracts through to the end of the sequence.