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# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

如果 source 包含的元素少于 count ,则返回空 IEnumerable<T> 。 如果 count 小于或等于零,则生成 的所有 source 元素。

TakeSkip 方法是功能补充。 给定一个集合序列 coll 和一个整数 n,将 和 的结果 coll.Take(n) 连接在一 coll.Skip(n) 起,生成与 相同的序列 coll

在 Visual Basic 查询表达式语法中, Skip 子句转换为 的调用 Skip

适用于

另请参阅