![](/img/trans.png)
[英]Why are the methods groups allowed on the left-side of ‘is’ operator and how can this be used in practice?
[英]why are lambdas & anonymous methods not allowed on the left side of the is or as operator?
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指出is
和as
不能與匿名方法和lambda表達式,因為他們已經沒有類型直到他們infered到一些實際的委托類型或表達式目錄樹中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.