C#中λ表達式

using System;
class B
{
public delegate string Make();
public delegate string Reply(string str);
 
 
public static void Main(){
Make  m= A.make;
Reply r = (str => A.reply(str));//λ表達式 現在r(string str)就代表A.reply(string str)
 
 
Console.WriteLine(kk(m));
Console.WriteLine(r("bingo reply"));
 
 
}
 
public static string kk(Make mm){
return mm();
}
}
 
class A
{
public static  string make(){
return "A make";
}
public static string did(){
return "A did";
}
public static string pick(){
return "A pick";
}
public static string reply(string str){
return str;
}
}
=========================================
λ表達式也可以做匿名方法
例如
 
delegate bool Method(int x,int y);
static void Main(){
Method method=(x,y)=>
{
return x>y;
};
 
Console.WriteLine(method(1,2));
}
 
輸出 false
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章