Csharp/CSharp Tutorial/LINQ/Reverse

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

An Example Calling the Reverse Operator

<source lang="csharp">using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {

   public static void Main() {
       string[] presidents = {"lore", "Truman", "Tailer", "Van", "Washington", "Wilson"};
       IEnumerable<string> items = presidents.Reverse();
       foreach (string item in items)
           Console.WriteLine(item);
   }

}</source>

Get all reversed cars

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.Xml.Linq; using System.Linq;

   class Car
   {
       public string PetName;
       public string Color;
       public int Speed;
       public string Make;
       
       public override string ToString()
       {
           return string.Format("Make={0}, Color={1}, Speed={2}, PetName={3}",Make, Color, Speed, PetName);
       }
   }
   class Program
   {
       static void Main(string[] args)
       {
           Car[] myCars = new []{
               new Car{ PetName = "A", Color = "Silver", Speed = 100, Make = "BMW"},
               new Car{ PetName = "B", Color = "Black", Speed = 55, Make = "VW"},
               new Car{ PetName = "C", Color = "White", Speed = 43, Make = "Ford"}
           };
       
           var subset = (from c in myCars select c).Reverse<Car>();
           foreach (Car c in subset)
           {
               Console.WriteLine(c.ToString());
           }
       
       }
   }</source>

Reverse does exactly as it says

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; public class MainClass {

   public static void Main() {
       int[] numbers = { 10, 9, 8, 7, 6 };
       IEnumerable<int> reversed = numbers.Reverse();    // { 6, 7, 8, 9, 10 }
   }

}</source>