簡體   English   中英

為什么在is或as運算符的左側不允許使用lambda和匿名方法?

[英]why are lambdas & anonymous methods not allowed on the left side of the is or as operator?

is或as運算符的左側不允許使用Lambda。 MSDN

一個真實的例子清楚的解釋將不勝感激?

Lambda沒有類型,因此,使用檢查沒有type的值的類型的運算符是沒有意義的。

我懷疑這與以下情況無關:

Func<string> x = () => "";  
bool result = x is Func<string>;

但是對於這種情況:

// This won't compile
if((() => "") is Func<string>)
{
}

...要么:

// This won't compile too
Func<string> func = (() => "") as Func<string>;

Lambda表達式和匿名方法本身沒有類型,但是在使用委托類型自動推斷時它們很有用:

// C# compiler understands that the right part should be Func<string>
// because the expression signature and return value matches Func<string>
Func<string> func = () => "hello world";

MSDN指出isas不能與匿名方法和lambda表達式,因為他們已經沒有類型直到他們infered到一些實際的委托類型或表達式目錄樹中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM