泛型委托
.Net 3.5定义了几个Action和Func的泛型委托,基本上可以满足我们常用的需求:
泛型委托的分类
第一组:无返回值
1 public delegate void Action()
2 public delegate void Action<T>(T obj)
3 public delegate void Action<T1, T2>(T1 arg1, T2 arg2)
4 public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
5 public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
2 public delegate void Action<T>(T obj)
3 public delegate void Action<T1, T2>(T1 arg1, T2 arg2)
4 public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
5 public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
第二组:有返回值
1 public delegate TResult Func<TResult>()
2 public delegate TResult Func<T, TResult>(T arg)
3 public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2)
4 public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3)
5 public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
2 public delegate TResult Func<T, TResult>(T arg)
3 public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2)
4 public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3)
5 public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
第三组:特定返回值
public delegate bool Predicate<T>(T obj);
该委托等价于
public delegate bool Func<T, bool>(T arg)
泛型委托在类库的使用
List<T>.ForEach(Action<T>(T obj) action);
1 namespace HHEC.willem
2 {
3 [TestFixture]
4 public class Genericdelegate
5 {
6 [Test]
7 public void TestListForEach()
8 {
9 List<String> names = new List<String>() { "Bruce", "Alfred", "Tim", "Richard" };
10 // Display the contents of the list using the Print method.
11 names.ForEach(Print);
12 // The following demonstrates the anonymous method feature of C#
13 // to display the contents of the list to the console.
14 names.ForEach(delegate(String name) { Console.WriteLine(name); });
15 //Display the contents of the list using the lambda.
16 names.ForEach(name => Console.WriteLine(name));
17 }
18
19 private void Print(string s){
20 Console.WriteLine(s);
21 }
22 }
23 }
2 {
3 [TestFixture]
4 public class Genericdelegate
5 {
6 [Test]
7 public void TestListForEach()
8 {
9 List<String> names = new List<String>() { "Bruce", "Alfred", "Tim", "Richard" };
10 // Display the contents of the list using the Print method.
11 names.ForEach(Print);
12 // The following demonstrates the anonymous method feature of C#
13 // to display the contents of the list to the console.
14 names.ForEach(delegate(String name) { Console.WriteLine(name); });
15 //Display the contents of the list using the lambda.
16 names.ForEach(name => Console.WriteLine(name));
17 }
18
19 private void Print(string s){
20 Console.WriteLine(s);
21 }
22 }
23 }
给Action<T>(T obj) action传值,使用了三种方式:
- 定义一个方法
- 匿名方法
- Lambda表达式
List<T>使用了Predicate<T>(T obj)的方法
1 public bool Exists(Predicate<T> match);
2 public T Find(Predicate<T> match);
3 public List<T> FindAll(Predicate<T> match);
4 public int FindIndex(Predicate<T> match);
5 public int FindIndex(int startIndex, Predicate<T> match);
6 public int FindIndex(int startIndex, int count, Predicate<T> match);
7 public T FindLast(Predicate<T> match);
8 public int FindLastIndex(Predicate<T> match);
9 public int FindLastIndex(int startIndex, Predicate<T> match);
10 public int FindLastIndex(int startIndex, int count, Predicate<T> match);
11 public int RemoveAll(Predicate<T> match);
12 public bool TrueForAll(Predicate<T> match);
2 public T Find(Predicate<T> match);
3 public List<T> FindAll(Predicate<T> match);
4 public int FindIndex(Predicate<T> match);
5 public int FindIndex(int startIndex, Predicate<T> match);
6 public int FindIndex(int startIndex, int count, Predicate<T> match);
7 public T FindLast(Predicate<T> match);
8 public int FindLastIndex(Predicate<T> match);
9 public int FindLastIndex(int startIndex, Predicate<T> match);
10 public int FindLastIndex(int startIndex, int count, Predicate<T> match);
11 public int RemoveAll(Predicate<T> match);
12 public bool TrueForAll(Predicate<T> match);
使用示例,参照MSDN
1 [Test]
2 public void TestListFind()
3 {
4 List<string> dinosaurs = new List<string>() {
5 "Compsognathus","Amargasaurus","Oviraptor","Velociraptor",
6 "Deinonychus","Dilophosaurus","Gallimimus","Triceratops"};
7 Console.WriteLine();
8 foreach (string dinosaur in dinosaurs)
9 {
10 Console.WriteLine(dinosaur);
11 }
12
13 Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
14 //如果 List<(Of <(T>)>) 中的每个元素都与指定的
15 //谓词所定义的条件相匹配,则为 true;否则为 false。
16 //如果列表不包含任何元素,则返回值为 true。
17 dinosaurs.TrueForAll(EndsWithSaurus));
18
19 Console.WriteLine("\nFind(EndsWithSaurus): {0}",
20 //如果找到与指定谓词定义的条件匹配的第一个元素,
21 //则为该元素;否则为类型 T 的默认值。
22 dinosaurs.Find(EndsWithSaurus));
23
24 Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
25 //如果找到,则为与指定谓词所定义的条件相匹配
26 //的最后一个元素;否则为类型 T 的默认值。
27 dinosaurs.FindLast(EndsWithSaurus));
28
29 Console.WriteLine("\nFindAll(EndsWithSaurus):");
30 //如果找到,则为一个 List<(Of <(T>)>),
31 //其中包含与指定谓词所定义的条件相匹配的所有元素;
32 //否则为一个空 List<(Of <(T>)>)。
33 List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
34
35 foreach (string dinosaur in sublist)
36 {
37 Console.WriteLine(dinosaur);
38 }
39
40 Console.WriteLine(
41 "\n{0} elements removed by RemoveAll(EndsWithSaurus).",
42 dinosaurs.RemoveAll(EndsWithSaurus));
43
44 Console.WriteLine("\nList now contains:");
45 foreach (string dinosaur in dinosaurs)
46 {
47 Console.WriteLine(dinosaur);
48 }
49
50 Console.WriteLine("\nExists(EndsWithSaurus): {0}",
51 dinosaurs.Exists(EndsWithSaurus));
52 }
53
54 // Search predicate returns true if a string ends in "saurus".
55 private bool EndsWithSaurus(String s)
56 {
57 if ((s.Length > 5) &&
58 (s.Substring(s.Length - 6).ToLower() == "saurus"))
59 {
60 return true;
61 }
62 else
63 {
64 return false;
65 }
66 }
67
2 public void TestListFind()
3 {
4 List<string> dinosaurs = new List<string>() {
5 "Compsognathus","Amargasaurus","Oviraptor","Velociraptor",
6 "Deinonychus","Dilophosaurus","Gallimimus","Triceratops"};
7 Console.WriteLine();
8 foreach (string dinosaur in dinosaurs)
9 {
10 Console.WriteLine(dinosaur);
11 }
12
13 Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
14 //如果 List<(Of <(T>)>) 中的每个元素都与指定的
15 //谓词所定义的条件相匹配,则为 true;否则为 false。
16 //如果列表不包含任何元素,则返回值为 true。
17 dinosaurs.TrueForAll(EndsWithSaurus));
18
19 Console.WriteLine("\nFind(EndsWithSaurus): {0}",
20 //如果找到与指定谓词定义的条件匹配的第一个元素,
21 //则为该元素;否则为类型 T 的默认值。
22 dinosaurs.Find(EndsWithSaurus));
23
24 Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
25 //如果找到,则为与指定谓词所定义的条件相匹配
26 //的最后一个元素;否则为类型 T 的默认值。
27 dinosaurs.FindLast(EndsWithSaurus));
28
29 Console.WriteLine("\nFindAll(EndsWithSaurus):");
30 //如果找到,则为一个 List<(Of <(T>)>),
31 //其中包含与指定谓词所定义的条件相匹配的所有元素;
32 //否则为一个空 List<(Of <(T>)>)。
33 List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
34
35 foreach (string dinosaur in sublist)
36 {
37 Console.WriteLine(dinosaur);
38 }
39
40 Console.WriteLine(
41 "\n{0} elements removed by RemoveAll(EndsWithSaurus).",
42 dinosaurs.RemoveAll(EndsWithSaurus));
43
44 Console.WriteLine("\nList now contains:");
45 foreach (string dinosaur in dinosaurs)
46 {
47 Console.WriteLine(dinosaur);
48 }
49
50 Console.WriteLine("\nExists(EndsWithSaurus): {0}",
51 dinosaurs.Exists(EndsWithSaurus));
52 }
53
54 // Search predicate returns true if a string ends in "saurus".
55 private bool EndsWithSaurus(String s)
56 {
57 if ((s.Length > 5) &&
58 (s.Substring(s.Length - 6).ToLower() == "saurus"))
59 {
60 return true;
61 }
62 else
63 {
64 return false;
65 }
66 }
67

浙公网安备 33010602011771号