委托

1.委托的定义

1.1从数据结构来讲,委托和类一样是用户自定义类型

1.2从设计模式来讲,委托提供了方法的抽象。

1.3委托是类型,就好像类是类型一样。与类一样,委托类型必须在被用来创建变量以及类型对象之前声明。

delegate void MyDel(int x)。

2.Lambda表达式

MyDel del = delegate( int x) { return x; };//匿名方法
MyDel del2 = (int x) => {return x;};
Lambda表达式 MyDel del3 = x => {return x};//简写的Lambda表达式

3.委托再次抽象--func、Action

3.1Action用于没有返回值的方法(参数可以根据自己情况进行传递)
3.2Func恰恰相反用于有返回值的方法(同样参数根据自己情况情况)
3.3用法:
Action<string> BookAction = new Action<string>("函数名");
Func<string> RetBook = new Func<string>("函数名");

 

posted @ 2018-01-05 15:36  zouhp  阅读(152)  评论(0编辑  收藏  举报