Enumerable.Skip<TSource>(IEnumerable<TSource>, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
绕过序列中的指定数量的元素,然后返回其余元素。
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<T> 包含输入序列中指定索引之后发生的元素。
例外
source 是 null。
示例
下面的代码示例演示如何用于 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 元素,则返回一个空 IEnumerable<T> 。 如果 count 小于或等于零,则会生成所有元素 source 。
功能和TakeSkip方法是功能补充。 给定集合序列 coll 和整数 n,连接 coll.Take(n) 结果并 coll.Skip(n) 生成与相同的序列 coll。
在Visual Basic查询表达式语法中,Skip 子句转换为调用 Skip。