C# Enumerable Reverse

Description

Inverts the order of the elements in a sequence.

Syntax


public static IEnumerable<TSource> Reverse<TSource>(
  this IEnumerable<TSource> source
)

Parameters

  • TSource - The type of the elements of source.
  • source - A sequence of values to reverse.

Example

The following code example demonstrates how to use Reverse to reverse the order of elements in an array.


//w  w w.  j a va  2s . co  m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    char[] apple = { 'a', 'b', 'c', 'd', 'e' };

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

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

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable