三种泛型委托简介

1 泛型委托 Predicate<T>

  早在Framework 2.0 的时候,微软就为 List<T> 类添加了 Find、FindAll 、ForEach 等方法用作数据的查找。

  public T Find ( Predicate<T> match)
  public List<T> FindAll(Predicate<T> match)

  在这些方法中存在一个Predicate <T> 表达式,它是一个返回bool的泛型委托,能接受一个任意类型的对象作为参数。

  public delegate bool Predicate<T>(T obj)

  在下面例子中,Predicate 委托绑定了参数为Person类的方法Match作为查询条件,然后使用 FindAll 方法查找到合适条件的 List<Person> 集合。

 1     class Program

 2     {

 3         static void Main(string[] args)

 4         {

 5             List<Person> list = GetList();

 6             //绑定查询条件

 7             Predicate<Person> predicate = new Predicate<Person>(Match);

 8             List<Person> result = list.FindAll(predicate);

 9             Console.WriteLine(“Person count is : ” + result.Count);

10             Console.ReadKey();

11         }

12         //模拟源数据

13         static List<Person> GetList()

14         {

15             var personList = new List<Person>();

16             var person1 = new Person(1,"Leslie",29);

17             personList.Add(person1);

18             ........

19             return personList;

20         }

21         //查询条件

22         static bool Match(Person person)

23         {

24             return person.Age <= 30;

25         }

26     }

27 

28     public class Person

29     {

30         public Person(int id, string name, int age)

31         {

32             ID = id;

33             Name = name;

34             Age = age;

35         }

36 

37         public int ID

38         { get; set; }

39         public string Name

40         { get; set; }

41         public int Age

42         { get; set; }

43     }
 

2  泛型委托 Action

  Action<T> 的使用方式与 Predicate<T> 相似,不同之处在于 Predicate<T> 返回值为 bool , Action<T> 的返回值为 void。
  Action 支持0~16个参数,可以按需求任意使用。

  public delegate void Action()
  public delegate void Action<T1>(T1 obj1)
  public delegate void Action<T1,T2> (T1 obj1, T2 obj2)
  public delegate void Action<T1,T2,T3> (T1 obj1, T2 obj2,T3 obj3)
  ............
  public delegate void Action<T1,T2,T3,......,T16> (T1 obj1, T2 obj2,T3 obj3,......,T16 obj16)

 
 1         static void Main(string[] args)

 2         {

 3             Action<string> action=ShowMessage;

 4             action("Hello World");

 5             Console.ReadKey();

 6         }

 7 

 8         static void ShowMessage(string message)

 9         {

10             MessageBox.Show(message);

11         }

 

3 泛型委托 Func

  委托 Func 与 Action 相似,同样支持 0~16 个参数,不同之处在于Func 必须具有返回值

  public delegate TResult Func<TResult>()
  public delegate TResult Func<T1,TResult>(T1 obj1)
  public delegate TResult Func<T1,T2,TResult>(T1 obj1,T2 obj2)
  public delegate TResult Func<T1,T2,T3,TResult>(T1 obj1,T2 obj2,T3 obj3)
  ............
  public delegate TResult Func<T1,T2,T3,......,T16,TResult>(T1 obj1,T2 obj2,T3 obj3,......,T16 obj16)

 
 1         static void Main(string[] args)

 2         {

 3             Func<double, bool, double> func = Account;

 4             double result=func(1000, true);

 5             Console.WriteLine("Result is : "+result);

 6             Console.ReadKey();

 7         }

 8 

 9         static double Account(double a,bool condition)

10         {

11             if (condition)

12                 return a * 1.5;

13             else

14                 return a * 2;

15         }

你可能感兴趣的:(泛型)