Enumerable.Skip<TSource>(IEnumerable<TSource>, Int32) 方法

定義

略過序列中指定數目的專案,然後傳回其餘專案。

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)

類型參數

TSource

元素 source的類型。

參數

source
IEnumerable<TSource>

一個 IEnumerable<T> 回歸元素的來源。

count
Int32

在返回剩餘元素前,需跳過的元素數量。

傳回

IEnumerable<TSource>

一個 IEnumerable<T> 包含輸入序列中指定索引之後的元素。

例外狀況

sourcenull

範例

以下程式碼範例示範如何跳 Skip 過陣列中指定數量的元素並回傳剩餘元素。

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

備註

此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。

source 包含少 count 於元素,則回傳 empty IEnumerable<T> 。 若 count 小於或等於零,則 的所有 source 元素皆可產生。

TakeSkip方法是功能互補。 給定一個集合序列coll和一個整數n,將 和 的結果coll.Take(n)coll.Skip(n)串接起來,得到與 相同的序列。coll

在Visual Basic查詢表達式語法中,Skip子句可轉譯為Skip的呼叫。

適用於

另請參閱