lamda表示式
格式:( 形參列表 ) =>
作用:簡化匿名方法的書寫,可用在任何可使用匿名方法和強型別**的地方;
例子:
using
system;
using
system.collections.generic;
using
system.linq;
using
system.text;
namespace
lamda_expression
", myarg);
return
"anonymous method done
"; };
d1 +=
((string
myarg)
=>
", myarg);
return
"lamda ok
"; });
console.writeline(
"d1 output:
", d1(
"arg
"));}}
} 輸出:anonymous method output: arg
lamda expression output: arg
d1 output: lamda ok
1. 普通繫結:
public2. 匿名委託:void button1_click(object
sender, eventargs e)
this.button1.click += button1_click;
this.button1.click += delegate(object3. lamda表示式:sender, eventargs e)
;
this.button1.click += ((sender, e) =>編譯器會自動推算出表示式中的型別。);
4. 過濾條件:
listusers = new list();等同於:func
bool> predicate =(
(user) =>
);list
temps = users.where(predicate).tolist();
listtemps = users.where(p => p.userid > 100).tolist();
listtemps = (from p in users where p.userid > 100select p).tolist();
listusers = new list();單挑語句時不需要使用{},同時可以不使用return來返回結果。func
bool> predicate =(
(user) => user.userid > 100
); list
temps = users.where(predicate).tolist();
5. 排序:
listusers = new list();list
temp1 = users.orderby(p=>p.userid).tolist();
func
int> orderby = (user =>user.userid);
list
temp2 = users.orderby(orderby).tolist();
LAMDA表示式學習
1.普通繫結 public void button1 click object sender,eventargs e this.button1.click button1 click 2.匿名委託 this.button1.click delegate object sender,eventargs...
python的lamda表示式
學習python時看到了這個函式 對裡面的lamda表示式產生了疑問。經過一番查詢,對lamda表示式理解如下 lamda看似是表示式,其實是乙個函式,冒號前是傳入的引數,冒號後是表示式,return將返回它的值。可以用來代替乙個簡單的函式,減少 篇幅 例1 def sum x,y return x...
lamda表示式之 orElseGet
未使用orelseget之前的 listresourceitems response.getdata if collectionutils.isnotempty resourceitems return e collect collectors.groupingby resourceitem get...