Enumerable.Reverse<TSource>(IEnumerable<TSource>) 方法

定义

反转序列中元素的顺序。

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

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要反转的值序列。

返回

IEnumerable<TSource>

一个序列,其元素以相反顺序对应于输入序列的元素。

例外

sourcenull

示例

下面的代码示例演示如何使用 Reverse 来反转数组中元素的顺序。

char[] apple = { 'a', 'p', 'p', 'l', 'e' };

char[] reversed = apple.Reverse().ToArray();

foreach (char chr in reversed)
{
    Console.Write(chr + " ");
}
Console.WriteLine();

/*
 This code produces the following output:

 e l p p a
*/
' Create a List of Char values.
Dim appleLetters As New List(Of Char)(New Char() _
                                  {"a"c, "P"c, "P"c, "L"c, "E"c})

' Reverse the order of the elements in the list.
' (We have to call AsEnumerable() in order to
' use System.Linq.Enumerable's Reverse() method.
Dim reversed() As Char =
appleLetters.AsEnumerable().Reverse().ToArray()

Dim output As New System.Text.StringBuilder
For Each chr As Char In reversed
    output.Append(chr & " ")
Next

' Display the output.
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' E L P P a

注解

此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 在通过直接调用GetEnumerator其方法或在 C# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

与 不同 OrderBy,此排序方法在确定顺序时不考虑实际值本身。 相反,它只是按基础源从中生成元素的相反顺序返回元素。

适用于