Method Slice
| Edit this page View SourceSlice<T>(IEnumerable<T>, int, int)
Extracts a contiguous count of elements from a sequence at a particular zero-based starting index
Declaration
public static IEnumerable<T> Slice<T>(this IEnumerable<T> source, int startIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence from which to extract elements |
int | startIndex | The zero-based index at which to begin slicing |
int | count | The number of items to slice out of the index |
Returns
Type | Description |
---|---|
IEnumerable<T> | A new sequence containing any elements sliced out from the source sequence |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the source sequence |
Remarks
If the starting position or count specified result in slice extending past the end of the sequence, it will
return all elements up to that point. There is no guarantee that the resulting sequence will contain the
number of elements requested - it may have anywhere from 0 to count
.
Examples
The following code example demonstrates how to take a range of elements from a sequence using Slice
.
var sequence = Enumerable.Range(1, 10);
// Take a slice of the sequence
var result = sequence.Slice(2, 3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [3, 4, 5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|