Windows 窗体扩展方法 Invoke()
不接受 lambda 表达式,我们无需先将其类型转换为委托(delegate)类型,如 Action
。这让我想知道,如果 lambda 表达式(带有主体)既不是明确的委托(delegate)也不是表达式,那么它的类型是什么?
最佳答案
This makes me wonder, if lambda expression (with a body) is not explicitly a delegate nor an expression, what is its type?
它没有通常意义上的类型(即 CLR 类型),就像 null
没有类型一样。 (旧版本的 C# 规范具有“空类型”的概念,但已被删除。)
相反,它是一个可转换为任何兼容的具体委托(delegate)或表达式树类型的表达式。
有关详细信息,请参阅 C# 5 规范的第 7.1 节(“表达式分类”)- 相关的要点(在表达式种类列表之外)是:
- A null literal. An expression with this classification can be implicitly converted to a reference type or nullable type.
- An anonymous function. An expression with this classification can be implicitly converted to a compatible delegate type or expression tree type.
关于c# - 带有主体的 lambda 表达式的类型到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25179802/