泛型List<T>使用示例

2018-01-22 16:45:14 浏览数 (1)

代码来源于"c#高级编程(第4版)",略作修改

代码语言:javascript复制
using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;





namespace Wrox.ProCSharp.VectorAsCollection

{

    class MainEntryPoint

    {

        static void Main(string[] args)

        {



            List<Racer> racers = new List<Racer>();

            racers.Add(new Racer("Michael Schumacher", "Ferriai"));

            racers.Add(new Racer("Juan Pablo Montoya", "McLaren-Mercedes"));

            racers.Add(new Racer("Kimi Railkkonen", "McLaren-Mercedes"));

            racers.Add(new Racer("Mark Webber", "Williams-BMW"));

            racers.Add(new Racer("Rubens Barichello", "Ferriai"));



            //遍历List

            foreach (Racer R in racers)

            {

                Console.WriteLine(R);

            }



            Console.WriteLine("n----------FindAll Method Demo-------------n");



            //查找findAll

            FindRacer finder = new FindRacer("Ferriai");

            foreach (Racer r in racers.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))//Predicate是一个泛型的委托

            //foreach (Racer r in racers.FindAll(new Predicate<Racer>(FindFerriai)))//就本例而言,查找开Farriai的Racer,也可以这样写,但这样不灵活,没办法将参数传入委托

            {

                Console.WriteLine(r);

            }



            Console.WriteLine("n----------Find Method Demo-------------");

            //Find方法(若找到的结果不止一条时,显示第一条)

            FindRacer f2 = new FindRacer("McLaren-Mercedes");



            Racer R2 = racers.Find(new Predicate<Racer>(f2.DrivingCarPredicate));



            if (R2 != null)

            {

                Console.WriteLine(R2);

            }

            else

            {

                Console.WriteLine("No racer drive McLaren-Mercedes ");

            }



            Console.WriteLine("n----------FindLast Method Demo-------------");

            FindRacer f3 = new FindRacer("McLaren-Mercedes");



            Racer R3 = racers.FindLast(new Predicate<Racer>(f2.DrivingCarPredicate));



            if (R3 != null)

            {

                Console.WriteLine(R3);

            }

            else

            {

                Console.WriteLine("No racer drive McLaren-Mercedes ");

            }



            Console.ReadLine();

        }





        //static bool FindFerriai(Racer R)

        //{

        //    return R.Car == "Ferriai";

        //}





    }





    /**/

    /// <summary>

    /// 查找Racer的类(根据car查找)

    /// </summary>

    public class FindRacer

    {

        private string _car;

        public FindRacer(string car)

        {

            this._car = car;

        }



        public bool DrivingCarPredicate(Racer r)

        {

            return r.Car == _car;

        }

    }



    /**/

    /// <summary>

    /// Racer类

    /// </summary>

    public class Racer

    {

        private string _name;

        public string Name

        {

            get

            {

                return _name;

            }

        }



        private string _car;

        public string Car

        {

            get

            {

                return _car;

            }

        }



        public Racer(string name, string car)

        {

            this._name = name;

            this._car = car;

        }



        public override string ToString()

        {

            return _name   ","   _car;

        }



    }

}

运行结果:

Michael Schumacher,Ferriai Juan Pablo Montoya,McLaren-Mercedes Kimi Railkkonen,McLaren-Mercedes Mark Webber,Williams-BMW Rubens Barichello,Ferriai

----------FindAll Method Demo-------------

Michael Schumacher,Ferriai Rubens Barichello,Ferriai

----------Find Method Demo------------- Juan Pablo Montoya,McLaren-Mercedes

----------FindLast Method Demo------------- Kimi Railkkonen,McLaren-Mercedes

0 人点赞