有趣的匿名方法

 好久没有关注语法了,随着接手一位同事的工作,发现他用了大量的匿名方法:
 例如 :

var remove = new Action<Tuple<string, int>>(k =>
{

Console.WriteLine(k.Item1);

Console.WriteLine(k.Item2);

});

Tuple <string,int> T=new Tuple<string ,int>("fadfdas",33);
remove(T);

----------------------------------------------------------------------------------------

这里相当于直接写了一个方法,这个方法

 void remove(Tuple<string ,int> k)

{

Console.WriteLine(k.Item1);

Console.WriteLine(k.Item2);

}

--------------------------------------不过我们同时还会遇到另外一种匿名方法,就是带有返回参数的-------------------------------

这里action就满足不了我们的需求,

var return1 = new Func<string, int,int>((b,c)=>

{
Console.WriteLine(b);
Console.WriteLine(c);

return 1;
});

Console.WriteLine(return1("333", 2));

-------------------------------------------以下是输出结果-----------------------------------------------------------------------------------

fadfdas
33
333
2
1

原文地址:https://www.cnblogs.com/chenli0513/p/3182632.html