[英]what the sign “=>” mean in C#?
我有以下代碼,但我不完全了解那里發生的事情:
Authorize auth = new Authorize(
this.google,
(DesktopConsumer consumer, out string requestToken) =>
GoogleConsumer.RequestAuthorization(
consumer,
GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger,
out requestToken));
這是我所知道的:
“授權”-只有1個接受2個參數的構造函數:(DesktopConsumer,FetchUri)。
“ this.google”-是“ desktopConsumer”對象。
“ GoogleConsumer.RequestAuthorization”返回一個“ Uri”對象。
我不明白這行是什么意思:
(DesktopConsumer consumer, out string requestToken) =>
在中間。
在這種情況下, =>
使用帶參數DesktopConsumer consumer, out string requestToken
的lambda表達式創建一個匿名方法/委托DesktopConsumer consumer, out string requestToken
。
=>
運算符有時稱為“轉到”運算符。 它是創建匿名方法的lambda語法的一部分。 運算符的左邊是方法的參數,右邊是實現。
在這里查看MSDN:
所有lambda表達式都使用lambda運算符=>,它被讀為“ goes to”。 lambda運算符的左側指定輸入參數(如果有),而右側則保存表達式或語句塊。 讀取lambda表達式x => x * x“ x達到x的x倍”。
暫時沒有這個問題問這個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/zh-cn/library/bb397687.aspx
它的英文意思是“翻譯為”。 它構成了lambda表達式的一部分: http : //msdn.microsoft.com/en-us/library/bb397687.aspx
它是lambda函數。 ()部分定義要傳遞給它的參數,而=>之后的部分是要評估的部分。
在您的情況下,lambda運算符的意思是“使用這些參數,執行以下代碼”。
因此,它本質上定義了一個匿名函數,您可以將DesktopConsumer和一個字符串(也可以在該函數中進行修改並發送回)傳遞給該匿名函數。
在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(請參閱缺少的括號())。 不是將Anonymous的結果作為參數傳遞,而是將Anonymous本身作為委托傳遞。 Authorize會在某個時候調用Anonymous並將其傳遞給實際參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.