Enumerable.Skip<TSource>(IEnumerable<TSource>, Int32) Metoda

Definicja

Pomija określoną liczbę elementów w sekwencji, a następnie zwraca pozostałe elementy.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ Skip(System::Collections::Generic::IEnumerable<TSource> ^ source, int count);
public static System.Collections.Generic.IEnumerable<TSource> Skip<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int count);
static member Skip : seq<'Source> * int -> seq<'Source>
<Extension()>
Public Function Skip(Of TSource) (source As IEnumerable(Of TSource), count As Integer) As IEnumerable(Of TSource)

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element do IEnumerable<T> zwracania elementów.

count
Int32

Liczba elementów do pominięcia przed zwróceniem pozostałych elementów.

Zwraca

IEnumerable<TSource>

Element IEnumerable<T> zawierający elementy, które występują po określonym indeksie w sekwencji danych wejściowych.

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak pominąć Skip określoną liczbę elementów w tablicy i zwrócić pozostałe elementy.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

Console.WriteLine("All grades except the first three:");
foreach (int grade in grades.Skip(3))
{
    Console.WriteLine(grade);
}

/*
 This code produces the following output:

All grades except the first three:
 56
 92
 98
 85
*/
' Create an array of integers that represent grades.
Dim grades() As Integer = {59, 82, 70, 56, 92, 98, 85}

' Sort the numbers in descending order and
' get all but the first (largest) three numbers.
Dim skippedGrades As IEnumerable(Of Integer) =
grades _
.Skip(3)

' Display the results.
Dim output As New System.Text.StringBuilder("All grades except the first three are:" & vbCrLf)
For Each grade As Integer In skippedGrades
    output.AppendLine(grade)
Next
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' All grades except the first three are:
' 56
' 92
' 98
' 85

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Jeśli source zawiera mniej niż count elementy, zwracana jest pusta IEnumerable<T> wartość. Jeśli count wartość jest mniejsza niż lub równa zero, wszystkie elementy source są zwracane.

Metody Take i Skip są uzupełnieniem funkcjonalnym. Biorąc pod uwagę sekwencję coll kolekcji i liczbę całkowitą n, łączenie wyników coll.Take(n) i coll.Skip(n) zwraca tę samą sekwencję co coll.

W składni wyrażenia zapytania języka Visual Basic klauzula Skip tłumaczy się na wywołanie Skipelementu .

Dotyczy

Zobacz też