我有以下代码,我无法完全理解那里发生了什么:
Authorize auth = new Authorize(
this.google,
(DesktopConsumer consumer, out string requestToken) =>
GoogleConsumer.RequestAuthorization(
consumer,
GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger,
out requestToken));
这就是我所知道的:
“授权” - 只有一个构造函数接受2个参数:(DesktopConsumer,FetchUri)
“this.google” - 是“desktopConsumer”对象
“GoogleConsumer.RequestAuthorization”返回“Uri”对象。
我无法理解这条线的含义是什么:
(DesktopConsumer consumer, out string requestToken) =>
在中间。
答案 0 :(得分:7)
在这种情况下,=>
使用带有DesktopConsumer consumer, out string requestToken
参数的lambda expression创建匿名方法/委托。
答案 1 :(得分:4)
=>
运算符有时被称为“转到”运算符。它是lambda语法的一部分,其中创建了匿名方法。操作符左侧是方法的参数,右侧是实现。
请参阅MSDN here:
所有lambda表达式都使用lambda operator =>,它被读作“转到”。 lambda运算符的左侧指定输入参数(如果有),右侧包含表达式或语句块。 lambda表达式x => x * x读作“x转到x乘x。”
答案 2 :(得分:2)
这个问题没有时间问这个lambda表达式的标志=>
什么是Lambda表达式?
Lambda expression is replacement of the anonymous method avilable in C#2.0 Lambda expression can do all thing which can be done by anonymous method.
Lambda expression are sort and function consist of single line or block of statement.
请阅读:http://pranayamr.blogspot.com/2010/11/lamda-expressions.html
上阅读更多相关信息答案 3 :(得分:2)
这意味着用英语'翻译'。它构成了lambda表达式的一部分:http://msdn.microsoft.com/en-us/library/bb397687.aspx
答案 4 :(得分:2)
它是一个lambda函数。 ()部分定义传递给它的参数,以及后面的部分=>正在评估的是什么。
答案 5 :(得分:2)
在您的情况下,lambda运算符意味着“使用这些参数,执行以下代码”。
所以它从根本上定义了一个匿名函数,你可以传递一个DesktopConsumer和一个字符串(也可以在函数中修改并发回)。
答案 6 :(得分:2)
“=>”用于lamdba表达式。
(DesktopConsumer consumer, out string requestToken) =>
GoogleConsumer.RequestAuthorization(
consumer,
GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger,
out requestToken)
是一种非常简短的方法声明形式,其方法名称是未知的(该方法是匿名的)。您可以通过以下方式替换此代码:
private void Anonymous (DesktopConsumer consumer, out string requestToken)
{
return GoogleConsumer.RequestAuthorization(
consumer,
GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger,
out requestToken);
}
然后通过以下方式替换呼叫:
Authorize auth = new Authorize(this.google, Anonymous);
请注意,此处不调用Anonymous(请参阅缺少的括号())。不是匿名的结果作为参数传递,但匿名本身作为委托传递。授权将在某个时刻调用匿名并传递实际参数。
答案 7 :(得分:1)
有关详细信息,请参阅我的other answer。