C# list 反轉 [Reverse()]

C# list 反轉 [Reverse()]

C# list 反轉 [Reverse()]


資料來源: https://www.cnblogs.com/Peng18233754457/p/8619012.html


Code

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Parasauralophus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Coelophysis");
        dinosaurs.Add("Oviraptor");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        dinosaurs.Reverse();

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        dinosaurs.Reverse(1, 4);

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Pachycephalosaurus
Parasauralophus
Mamenchisaurus
Amargasaurus
Coelophysis
Oviraptor

Oviraptor
Coelophysis
Amargasaurus
Mamenchisaurus
Parasauralophus
Pachycephalosaurus

Oviraptor
Parasauralophus
Mamenchisaurus
Amargasaurus
Coelophysis
Pachycephalosaurus
 */

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *