工厂4.0与生产自动化

十五年的从业经验,专注于半导体封装测试领域的生产自动化,提供相关软件产品及服务。 www.sevenswords.cn
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Does Lamda expression return value?

Posted on 2016-01-17 21:48  东大叔  阅读(233)  评论(0编辑  收藏  举报

Basically, the compiler does this for you.

 

If you write a lambda as a single statement (and don't include block notation, ie: {}), the returned value is the value of the expression written.

 

In your case, this:

Func<int,int> square = x => x*x;

 

Is seen to only have one expression (x*x), so it is treated as:

Func<int,int> square = (int x) => { return x*x; };

 

If you want to have more than a single statement in the lambda, you'd need the braces, in which case you'd have to write the return for it to compile correctly.