泛型List<T>使用示例
代码来源于"c#高级编程(第4版)",略作修改
运行结果:
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Text;
5![](/Images/OutliningIndicators/None.gif)
6![](/Images/OutliningIndicators/None.gif)
7
namespace Wrox.ProCSharp.VectorAsCollection
8
{
9
class MainEntryPoint
10
{
11
static void Main(string[] args)
12
{
13![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
21
//遍历List
22
foreach (Racer R in racers)
23
{
24
Console.WriteLine(R);
25
}
26![](/Images/OutliningIndicators/InBlock.gif)
27
Console.WriteLine("\n----------FindAll Method Demo-------------\n");
28![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
37
Console.WriteLine("\n----------Find Method Demo-------------");
38
//Find方法(若找到的结果不止一条时,显示第一条)
39
FindRacer f2 = new FindRacer("McLaren-Mercedes");
40![](/Images/OutliningIndicators/InBlock.gif)
41
Racer R2 = racers.Find(new Predicate<Racer>(f2.DrivingCarPredicate));
42![](/Images/OutliningIndicators/InBlock.gif)
43
if (R2 != null)
44
{
45
Console.WriteLine(R2);
46
}
47
else
48
{
49
Console.WriteLine("No racer drive McLaren-Mercedes ");
50
}
51![](/Images/OutliningIndicators/InBlock.gif)
52
Console.WriteLine("\n----------FindLast Method Demo-------------");
53
FindRacer f3 = new FindRacer("McLaren-Mercedes");
54![](/Images/OutliningIndicators/InBlock.gif)
55
Racer R3 = racers.FindLast(new Predicate<Racer>(f2.DrivingCarPredicate));
56![](/Images/OutliningIndicators/InBlock.gif)
57
if (R3 != null)
58
{
59
Console.WriteLine(R3);
60
}
61
else
62
{
63
Console.WriteLine("No racer drive McLaren-Mercedes ");
64
}
65![](/Images/OutliningIndicators/InBlock.gif)
66
Console.ReadLine();
67
}
68![](/Images/OutliningIndicators/InBlock.gif)
69![](/Images/OutliningIndicators/InBlock.gif)
70
//static bool FindFerriai(Racer R)
71
//{
72
// return R.Car == "Ferriai";
73
//}
74![](/Images/OutliningIndicators/InBlock.gif)
75![](/Images/OutliningIndicators/InBlock.gif)
76
}
77![](/Images/OutliningIndicators/InBlock.gif)
78![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
91
public bool DrivingCarPredicate(Racer r)
92
{
93
return r.Car == _car;
94
}
95
}
96![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
112
private string _car;
113
public string Car
114
{
115
get
116
{
117
return _car;
118
}
119
}
120![](/Images/OutliningIndicators/InBlock.gif)
121
public Racer(string name, string car)
122
{
123
this._name = name;
124
this._car = car;
125
}
126![](/Images/OutliningIndicators/InBlock.gif)
127
public override string ToString()
128
{
129
return _name + "," + _car;
130
}
131![](/Images/OutliningIndicators/InBlock.gif)
132
}
133
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
98
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
110
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
119
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
126
![](/Images/OutliningIndicators/InBlock.gif)
127
![](/Images/OutliningIndicators/InBlock.gif)
128
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
131
![](/Images/OutliningIndicators/InBlock.gif)
132
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
133
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
运行结果:
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
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。