C#委托基础6——泛型委托Predicate

来源:互联网 发布:php判断是不是正整数 编辑:程序博客网 时间:2024/05/23 11:17

此委托返回一个bool值,该委托通常引用一个"判断条件函数"。需要指出的是,判断条件一般为“外部的硬性条件”,比如“大于50”,而不是由数据自身指定,不如“查找数组中最大的元素就不适合”。

 

例一

[csharp] view plaincopyprint?
  1. class Program  
  2. {  
  3.         bool IsGreaterThan50(int i)  
  4.         {  
  5.             if (i > 50)  
  6.                 return true;  
  7.             else  
  8.                 return false;  
  9.         }  
  10.   
  11.         static void Main(string[] args)  
  12.         {  
  13.             Program p=new Program();  
  14.   
  15.             List<int> lstInt = new List<int>();  
  16.             lstInt.Add(50);  
  17.             lstInt.Add(80);  
  18.             lstInt.Add(90);  
  19.   
  20.             Predicate<int> pred = p.IsGreaterThan50;  
  21.              
  22.             int i = lstInt.Find(pred);                         // 找到匹配的第一个元素,此处为80  
  23.             Console.WriteLine("大于50的第一个元素为{0}",i);  
  24.   
  25.              
  26.   
  27.             List<int> all = lstInt.FindAll(pred);  
  28.             for (int j = 0; j < all.Count(); j++)  
  29.             {  
  30.                 Console.WriteLine("大于50的数组中元素为{0}", all[j]);  // 找出所有匹配条件的  
  31.             }  
  32.   
  33.             Console.ReadLine();  
  34.         }  
  35. }  

例二

[csharp] view plaincopyprint?
  1. class Staff  
  2. {  
  3.         private double salary;  
  4.   
  5.         public double Salary  
  6.         {  
  7.             get { return salary; }  
  8.             set { salary = value; }  
  9.         }  
  10.   
  11.         private string num;  
  12.   
  13.         public string Num  
  14.         {  
  15.             get { return num; }  
  16.             set { num = value; }  
  17.         }  
  18.   
  19.         public override string ToString()  
  20.         {  
  21.             return "Num......" + num + "......" + "......" + "Salary......" + salary;  
  22.         }  
  23. }  
  24.   
  25.    
  26.   
  27. class Program  
  28. {  
  29.         bool IsSalaryGreaterThan5000(Staff s)  
  30.         {  
  31.             if (s.Salary > 5000)  
  32.                 return true;  
  33.             else  
  34.                 return false;  
  35.         }  
  36.   
  37.         static void Main(string[] args)  
  38.         {  
  39.             Program p = new Program();  
  40.   
  41.             List<Staff> allStaff = new List<Staff>  
  42.             {  
  43.                 new Staff{Num="001",Salary=9999.9},  
  44.                 new Staff{Num="002",Salary=8991},  
  45.                 new Staff{Num="003",Salary=10000.8},  
  46.                 new Staff{Num="004",Salary=4999.99}  
  47.             };  
  48.   
  49.             Predicate<Staff> s = p.IsSalaryGreaterThan5000;  
  50.             Staff theFirstOne = allStaff.Find(s);  
  51.             Console.WriteLine(theFirstOne);              // 找出第一个  
  52.   
  53.             List<Staff> all = allStaff.FindAll(s);  
  54.             for (int i = 0; i < all.Count(); i++)  
  55.             {  
  56.                 Console.WriteLine(all[i]);              // 找出所有满足条件的  
  57.             }  
  58.   
  59.             Console.ReadLine();  
  60.         }  
  61. }  
0 0
原创粉丝点击