Beispiel #1
0
        static void Main()
        {
            var graham  = new Racer(7, "Graham", "Hill", "UK", 14);
            var emerson = new Racer(13, "Emerson", "Fittipaldi", "Brazil", 14);
            var mario   = new Racer(16, "Mario", "Andretti", "USA", 12);

            var racers = new List <Racer>(20)
            {
                graham, emerson, mario
            };

            racers.Add(new Racer(24, "Michael", "Schumacher", "Germany", 91));
            racers.Add(new Racer(27, "Mika", "Hakkinen", "Finland", 20));

            racers.AddRange(new Racer[] {
                new Racer(14, "Niki", "Lauda", "Austria", 25),
                new Racer(21, "Alain", "Prost", "France", 51)
            });

            var racers2 = new List <Racer>(new Racer[] {
                new Racer(12, "Jochen", "Rindt", "Austria", 6),
                new Racer(22, "Ayrton", "Senna", "Brazil", 41)
            });
        }
Beispiel #2
0
        static void ListDemo()
        {
            List <Racer> racers = new List <Racer>(20);

            Racer graham = new Racer("Graham", "Hill", "UK", 14);

            racers.Add(graham);
            Racer emerson = new Racer("Emerson", "Fittipaldi", "Brazil", 14);

            racers.Add(emerson);
            Racer mario = new Racer("Mario", "Andretti", "USA", 12);

            racers.Add(mario);
            racers.Add(new Racer("Michael", "Schumacher", "Germany", 91));
            racers.Add(new Racer("Mika", "Hakkinen", "Finland", 20));

            racers.AddRange(new Racer[] { new Racer("Niki", "Lauda", "Austria", 25), new Racer("Alain", "Prost", "France", 51) });



            List <Racer> racers2 = new List <Racer>(20)
            {
                graham, emerson, mario
            };

            Console.WriteLine(racers.Capacity);

            racers.Insert(3, new Racer("Phil", "Hill", "USA", 3));

            for (int i = 0; i < racers.Count; i++)
            {
                Console.WriteLine(racers[i]);
            }

            foreach (Racer r in racers)
            {
                Console.WriteLine(r);
            }

            //racers.ForEach(
            //   delegate(Racer r)
            //   {
            //      Console.WriteLine(r);
            //   });

            racers.ForEach(r => Console.WriteLine("{0:A}", r));

            int index1 = racers.IndexOf(mario);

            int index2 = racers.FindIndex(new FindCountry("Finland").FindCountryPredicate);

            //int index3 = racers.FindIndex(
            //   delegate(Racer r)
            //   {
            //      return r.LastName == "Rindt";
            //   });
            int index3 = racers.FindIndex(r => r.LastName == "Rindt");

            //Racer racer = racers.Find(
            //   delegate(Racer r)
            //   {
            //      return r.FirstName == "Niki";
            //   });

            Racer racer = racers.Find(r => r.FirstName == "Niki");

            //List<Racer> bigWinners = racers.FindAll(
            //   delegate(Racer r)
            //   {
            //      return r.Wins > 20;
            //   });
            List <Racer> bigWinners = racers.FindAll(r => r.Wins > 20);

            foreach (Racer r in bigWinners)
            {
                Console.WriteLine("{0:A}", r);
            }



            Racer j = new Racer("Jochen", "Rindt", "Austria", 6);

            if (racers.Remove(j))
            {
                Console.WriteLine("removed");
            }

            //List<Racer> w = racers.FindAll(
            //   delegate(Racer r)
            //   {
            //      return r.Wins > 15;
            //   });
            List <Racer> w = racers.FindAll(r => r.Wins > 15);

            Console.WriteLine("Won more than 15 races");
            w.ForEach(Console.WriteLine);

            racers.Sort();

            Console.WriteLine("sorted...");
            racers.ForEach(Console.WriteLine);

            racers.Sort(new RacerComparer(RacerComparer.CompareType.Country));


            racers.ForEach(Console.WriteLine);

            //List<Person> persons = racers.ConvertAll<Person>(
            //   delegate(Racer r)
            //   {
            //      return new Person(r.FirstName + " " + r.LastName);
            //   });
            List <Person> persons = racers.ConvertAll <Person>(
                r => new Person(r.FirstName + " " + r.LastName));

            racers.Sort((r1, r2) => r2.Wins.CompareTo(r1.Wins));


            //racers.ForEach(Console.WriteLine);
            //racers.Sort();
            //racers.ForEach(Console.WriteLine);
            //racers.Sort(delegate(Racer r1, Racer r2)
            //{
            //   return r2.Wins.CompareTo(r1.Wins);
            //});
            //racers.ForEach(Console.WriteLine);
        }