c# 委托与lambda用法

c# 委托与lambda用法

委托

如下写法:
delegate bool Function(int num);
返回值:bool,形式参数 :int,委托类型名:Function
以Function为类型名,可以写出不同的变量

如下:
Function fun = delegate (int n){ return n>0; };
使用方法可以如函数一样:
if(fun(x))
console.write(x + “大于0”);

lambda

是委托的简写方式
Function fun = delegate (int n){ return n>0; };
delegate 可以去掉,参数类型名也可以去掉:
Function fun = (n) => { return n>0; };
如果只有一条语句,可以去掉括号:
Function fun = n => return n>0;

可以与where一起使用:
foreach(int n in num.Where(n => n>0))
console.write(n);

发布了3 篇原创文章 · 获赞 0 · 访问量 53

猜你喜欢

转载自blog.csdn.net/qq_40915831/article/details/102654767
今日推荐