什么符号"=>"c#中的平均值

本文关键字:quot 平均值 符号 什么 | 更新日期: 2023-09-27 18:18:18

我有以下代码,我不能完全理解那里发生了什么:

Authorize auth = new Authorize(
    this.google,
    (DesktopConsumer consumer, out string requestToken) =>
    GoogleConsumer.RequestAuthorization(
        consumer,
        GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger,
        out requestToken));

这是我所知道的:
"Authorize" -只有1个构造函数,接受2个参数:(DesktopConsumer, FetchUri).
"这。是一个"desktopConsumer"对象。
"GoogleConsumer。RequestAuthorization"返回一个"Uri"对象。

我不明白这句话的意思:
(DesktopConsumer consumer, out string requestToken) =>
在中间

什么符号"=>"c#中的平均值

在本例中,=>使用带参数DesktopConsumer consumer, out string requestToken的lambda表达式创建了一个匿名方法/委托。

=>操作符有时被称为"转到"操作符。它是lambda语法的一部分,在其中创建匿名方法。操作符的左边是方法的实参,右边是实现。

见MSDN:

所有lambda表达式都使用lambda操作符=>,读取为"goes to"。lambda运算符的左侧指定输入参数(如果有的话),右侧保存表达式或语句块。lambda表达式x => x * x被读取为"x到x乘以x"。

它在英语中的意思是"翻译为"。它构成lambda表达式的一部分:http://msdn.microsoft.com/en-us/library/bb397687.aspx

这是一个lambda函数。()部分定义传递给它的参数,而=>之后的部分是正在计算的内容。

在您的示例中,lambda操作符表示"使用这些参数,执行以下代码"。

因此它本质上定义了一个匿名函数,您可以向该函数传递一个DesktopConsumer和一个字符串(也可以在函数中修改并返回)。

"=>"用于lambda表达式

(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(参见缺少的括号())。不是匿名的结果作为参数传递,而是匿名本身作为委托传递。Authorize将在某个时刻调用Anonymous并向其传递实际参数。

这个问题第一次问这个符号表示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

在MSDN上阅读更多信息:http://msdn.microsoft.com/en-us/library/bb397687.aspx

Lambda算子