.net 委托 +lamda表达式

1.委托与类同级 想当于定义了一个类型 如

delegate int Sum(int a, int b);// 在声明类之前声明

2.这里可以在类里写个函数

public int sumAB(int a,int b){return a+b}
//
//用法
 Sum a = new Sum(sumAB);
            a(1, 2);

3.用lamda表达式

  Sum sab = (a, b) => { return a + b; };

注下边两个不用声明 delagate类型

4.Action<T>可看成无反回值 的函数 且必须无反回值 最多16个参数

Action<string> ac = (a) => { MessageBox.Show(a + "很好"); };
 ac("");

5.Func<T,Tobject> 有反回值的 前边是参数 后边的反回的结果,最多16个参数+1个结果。最后无大括号,单语句时,可省掉return

 

 Func<string,string,string> fc =(a,b)=>{ return a + b; };
            MessageBox.Show(fc("1", "s"));
posted @ 2018-03-30 11:30  伪装大牛  阅读(221)  评论(0)    收藏  举报