c# - 带有主体的 lambda 表达式的类型到底是什么?

标签 c# .net lambda

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/

相关文章:

.net - .Net 项目所需的免费或商业 SCP 库

c# - EF Code First Migrations 创建额外的外键

java - Xtend lambda 表达式重载 Xtext QuickFix API

c# - 转换 Model3DGroup 两次

c# - C# 编译器如何知道何时停止异步方法?

python - 在 try-except block 中关闭游标后如何关闭连接?

java - 在 Java 8 中是否有一种优雅的方法来应用过滤器数组?

c# - 如何在 C# 中打开 Excel 文件?

c# - 在 C# 中从 int 中获取单独的数字

c# - 如何测试 MVC View ?