代码来源于"c#高级编程(第4版)",略作修改
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5
6
7namespace Wrox.ProCSharp.VectorAsCollection
8{
9 class MainEntryPoint
10 {
11 static void Main(string[] args)
12 {
13
14 List<Racer> racers = new List<Racer>();
15 racers.Add(new Racer("Michael Schumacher", "Ferriai"));
16 racers.Add(new Racer("Juan Pablo Montoya", "McLaren-Mercedes"));
17 racers.Add(new Racer("Kimi Railkkonen", "McLaren-Mercedes"));
18 racers.Add(new Racer("Mark Webber", "Williams-BMW"));
19 racers.Add(new Racer("Rubens Barichello", "Ferriai"));
20
21 //遍历List
22 foreach (Racer R in racers)
23 {
24 Console.WriteLine(R);
25 }
26
27 Console.WriteLine("\n----------FindAll Method Demo-------------\n");
28
29 //查找findAll
30 FindRacer finder = new FindRacer("Ferriai");
31 foreach (Racer r in racers.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))//Predicate是一个泛型的委托
32 //foreach (Racer r in racers.FindAll(new Predicate<Racer>(FindFerriai)))//就本例而言,查找开Farriai的Racer,也可以这样写,但这样不灵活,没办法将参数传入委托
33 {
34 Console.WriteLine(r);
35 }
36
37 Console.WriteLine("\n----------Find Method Demo-------------");
38 //Find方法(若找到的结果不止一条时,显示第一条)
39 FindRacer f2 = new FindRacer("McLaren-Mercedes");
40
41 Racer R2 = racers.Find(new Predicate<Racer>(f2.DrivingCarPredicate));
42
43 if (R2 != null)
44 {
45 Console.WriteLine(R2);
46 }
47 else
48 {
49 Console.WriteLine("No racer drive McLaren-Mercedes ");
50 }
51
52 Console.WriteLine("\n----------FindLast Method Demo-------------");
53 FindRacer f3 = new FindRacer("McLaren-Mercedes");
54
55 Racer R3 = racers.FindLast(new Predicate<Racer>(f2.DrivingCarPredicate));
56
57 if (R3 != null)
58 {
59 Console.WriteLine(R3);
60 }
61 else
62 {
63 Console.WriteLine("No racer drive McLaren-Mercedes ");
64 }
65
66 Console.ReadLine();
67 }
68
69
70 //static bool FindFerriai(Racer R)
71 //{
72 // return R.Car == "Ferriai";
73 //}
74
75
76 }
77
78
79 /**//**/
80 /**//// <summary>
81 /// 查找Racer的类(根据car查找)
82 /// </summary>
83 public class FindRacer
84 {
85 private string _car;
86 public FindRacer(string car)
87 {
88 this._car = car;
89 }
90
91 public bool DrivingCarPredicate(Racer r)
92 {
93 return r.Car == _car;
94 }
95 }
96
97 /**//**/
98 /**//// <summary>
99 /// Racer类
100 /// </summary>
101 public class Racer
102 {
103 private string _name;
104 public string Name
105 {
106 get
107 {
108 return _name;
109 }
110 }
111
112 private string _car;
113 public string Car
114 {
115 get
116 {
117 return _car;
118 }
119 }
120
121 public Racer(string name, string car)
122 {
123 this._name = name;
124 this._car = car;
125 }
126
127 public override string ToString()
128 {
129 return _name + "," + _car;
130 }
131
132 }
133}
运行结果:
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