Slice - Kalkwst/Risotto GitHub Wiki

Definition

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

Overloads

Overload Description
Slice(int start, int end) Extract a portion of the source sequence into a new sequence from start to end.

Slice(int start, int 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);

Type Parameters

TSource
The type of the elements of the original sequence

Parameters

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.

Returns

IEnumerable<TResult>
A new enumerable containing the extracted elements.

Exceptions

ArgumentNullException
source is null

Example

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" ]

Remarks

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.

⚠️ **GitHub.com Fallback** ⚠️