使用Lambda表达式重构委托

1.事件

通常写法(C# 1.0)

this.button.Click +=new EventHandler(button_Click); 

void button_Click(object sender, EventArgs e) 
{ 
     throw new NotImplementedException(); 
} 

在C#2.0中使用匿名方法

this.button.Click +=delegate{
throw new NotImplementedException(); 

}; 
//或者 
this.button.Click +=delegate(object obj, EventArgs args)

   throw new NotImplementedException(); 

}; 

使用Lamba表达式

this.button.Click += (s, ev) => { throw new NotImplementedException(); }; 

2.一般委托

 

 Func<int,int,int> max=(a,b)=>
 {
     if (a > b)
        return a;
      return b;
  };      
  int rst=max(222,134);
  Console.Write(rst)

参考自http://www.cnblogs.com/neozhu/archive/2010/07/16/1778864.html

posted @ 2012-04-25 10:35  Bug山Bug海  阅读(172)  评论(0)    收藏  举报