List<T>.Sort() // 排序T |
2 |
List<T>.Find() // 找出一個T |
3 |
List<T>.FindAll() //找出多個T |
4 |
List<T>.Exist() //判斷T是否存在 |
1 |
lIST<Person> lstPerson= new List<>(Person); |
01 |
//List<T>.Find() |
02 |
//找出Name='puma'的Person |
03 |
Response.Write( "找出Name='puma'的Person→ " ); |
04 |
Response.Write(lstPerson.Find( delegate (Person p) { return p.Name == "puma" ; }).ToString() + "<p>" ); |
05 |
|
06 |
|
07 |
|
08 |
//List<T>.FindAll() |
09 |
//找出Age>10的數目 |
10 |
Response.Write( "找出Age>10的數目→ " ); |
11 |
Response.Write(lstPerson.FindAll( delegate (Person p) { return p.Age > 10; }).Count.ToString() + "<p>" ); |
12 |
|
13 |
|
14 |
|
15 |
//List<T>.Exists() |
16 |
//檢查Name='F6'是否存在 |
17 |
Response.Write( "檢查Name='F6'是否存在→ " ); |
18 |
Response.Write(lstPerson.Exists( delegate (Person p) { return p.Name == "F6" ; }).ToString() + "<p>" ); |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
//List<T>.Sort() |
24 |
//依Name升冪排序 |
25 |
Response.Write( "<p>依Name升冪排序↑<br/>" ); |
26 |
lstPerson.Sort( delegate (Person p1, Person p2) { return Comparer< string >.Default.Compare(p1.Name, p2.Name); }); |
27 |
|
28 |
|
29 |
|
30 |
//List<T>.Sort() |
31 |
//依Name降冪排序 |
32 |
Response.Write( "<p>依Name降冪排序↓<br/>" ); |
33 |
lstPerson.Sort( delegate (Person p1, Person p2) { return Comparer< string >.Default.Compare(p2.Name, p1.Name); }); |
徐燕平