017天(.net学习之路

来源:互联网 发布:哥特穿衣 知乎 编辑:程序博客网 时间:2024/05/01 11:22

委托:将方法作为方法的参数

声明委托的方式:delegate 返回值类型 委托类型名(参数)
比如delegate void StringProcess(string s);
注意这里的除了前面的delegate,剩下部分和声明一个函数一样,但是StringProcess不是函数名,而是委托类型名
声明的委托是一种类型,就像int、Person一样,如果要用的话还要声明委托类型的变量,声明委托类型变量的方式:StringProcess f1;
将委托类型变量指向函数 StringProcess sp = new StringProcess(SayHello),这样就可以像调用普通函数一样把sp当成函数用了。委托可以看做是函数的指针。整数可以用整数变量指向它,对象可以用对象变量指向它,函数也可以用委托变量指向它。和直接调用函数的区别:用委托就可以指向任意的函数,哪怕是之前没定义的都可以,而不使用受限于那几种。
将委托类型变量指向函数还可以简化成StringProcess sp = SayHello,编译器帮我们进行了new。但是不能sp=PrintIt(),因为这样就成了函数调用。
 
 1 static void Main(string[] args) 2         { 3             //对指定的数组进行排序 4             object[] ob = { 84, 12, 5454, 112, 1, 33, 12, 266, 05, 45, 44 }; 5             //2、让委托指向方法 6             DelSort del = Int; 7             //3、把委托指向的方法传递到方法中 8             Sort(ob, del); 9             foreach (object item in ob)10             {11                 Console.WriteLine(item);12             }13 14             Console.ReadKey();15         }16         //排序 int类大小的方法17         static int Int(object o1, object o2)18         {19             int a = (int)o1;20             int b = (int)o2;21             return a - b;22         }23         //1、定义一个委托24         delegate int DelSort(object o1,object o2);25         static void Sort(object[] arr, DelSort del)26         {27             for (int i = 0; i < arr.Length-1; i++)28             {29                 for (int y = i; y < arr.Length; y++)30                 {31                     if (del(arr[i], arr[y]) > 0)32                     {33                         object temp = arr[i];34                         arr[i] = arr[y];35                         arr[y] = temp;36                     }37                 }38             }39         }40     }

 

 1  static void Main(string[] args) 2         { 3             //系统定义的委托使用方法,List<> 的RemoveAll方法 4             int[] i = { 5, 8, 9, 1, 2, 3, 4, 7, 444 }; 5             List<int> arr = new List<int>(); 6             arr.AddRange(i); 7             //Predicate<int> pre = Condition;也可以把这行注释了 8             Predicate<int> pre = Condition; 9             arr.RemoveAll(pre);10 11             foreach (int item in arr)12             {13                 Console.WriteLine(item);14             }15             Console.ReadKey();16         }17         //删除的条件大于518         static bool Condition(int o1)19         {20             if (o1  > 5)21             {22                 return true;23             }24             else25             {26                 return false;27             }28             29         }

----多播委托

delegate void ProcessWordDelegate(string s)
ProcessWordDelegate d = new ProcessWordDelegate(SayHello)+new ProcessWordDelegate(ToLower)
多播委托要求方法必须返回void
组合的委托必须是同一个类型
相当于创建了一个按照组合的顺序依次调用的新委托对象。
委托的组合一般是给事件用的,用普通的委托的时候很少用

<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击