class Program
{
static void Main(string[] args)
{
List<Person> persons = new List<Person>() {
new Person{ID=,Name="lin1"},
new Person{ID=,Name="lin2"},
new Person{ID=,Name="lin3"}
}; Person person = persons.Find(
delegate(Person p) //this is an anonymous method.
{
return p.ID == ;
}
);
Person p1 = persons.Find(p=>p.ID==);//using lambda expression
Person p2 = persons.Find((Person p)=>p.ID==);//you can also explicitly the input type but no required
Console.WriteLine("person id={0},name={1}", person.ID, person.Name); }
}
class Person
{
public int ID { get; set; }
public string Name { get; set; }
}

=> is called lambda operator and read as Goes To. Notice that with a lambda expression you don't have to use the delegate keyword explicitly and don't have to specify the input parameter type explicity. The parameter type is inferred(推倒出来). lambda expressions are more convenient to use than anonymous methods. lambda expressions are particularly helpful for writing LINQ query expressions.

In most of the cases lambda expressions supersedes(替代) anonymous methods. To my knowlege, the only time I prefer to use anonymous methods over lambdas is, when we have to omit(省略) the parameter list when it's not used within the body.

Anonymous methods allow the parameter list to be omitted entirely when it's not used within the body,where as with lambda expressions this is not the case.

For example, with anonymous method notice that we have omitted the parameter list as we are not using them within the body

Button.Click += delegate{MessageBox.Show("hello world.");};

The above code can be rewritten using lambda expression as shown below.Notice that with lambda we cannot omit the parameter list.

Button.Click+=(sender,e)=>{MessegeBox.Show("hello world.");};
Button.Click+=()=>{MessegeBox.Show("hello world.");};//if omit parameter list it will get a compilar error.

最新文章

  1. C#基础-out与ref字段
  2. NOIP2015 Revenge
  3. guava学习--File
  4. nodejs 针对 mysql 设计的原型库,支持事务/共享多次/单次查询
  5. JSON讲解和“弹窗”
  6. PHP上传图片如何防止图片木马?
  7. Android的px、dip、sp的区别
  8. [转帖]译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
  9. bootstrap 模态框关闭状态怎么获取
  10. PartialFunction(偏函数)
  11. MVC之MVCSQO方法查询、排序、分页、投影
  12. 基于WEB 的认证防火墙的设计
  13. centos7编译linux的内核源码
  14. 【模拟】[NOIP2011]铺地毯[c++]
  15. hdu4035 Maze
  16. EasyUI tab问题记录
  17. Java学习01 (第一遍)
  18. SVN OPS发布总结
  19. Easyui实用视频教程系列---Tree点击打开tab页面
  20. Centos中彻底删除Mysql(rpm、yum安装的情况)

热门文章

  1. Java数据结构之线性表
  2. C++ Primer 学习笔记_46_STL实践与分析(20)--容器特有的算法
  3. zoj-3626 Treasure Hunt I (树形dp)
  4. [AngularJS] Accessing Scope from The Console
  5. stm32出现错误“identifier file is undefined”
  6. 终端I/O之非规范模式
  7. nginx平滑重启与平滑升级的方法
  8. Linux 命令 alternatives和update-alternatives
  9. nmon的安装以及使用
  10. 解决java中对URL编码的问题