当前位置: 首页 > news >正文

Lambda表达式和匿名方法中不支持yield return

尝试写过,但是得到了编译错误:"The yield statement cannot be used inside an anonymous method or lambda expression". 我想不出不支持的理由,最终在网上找到了解释(http://stackoverflow.com/questions/1217729/in-c-why-cant-an-anonymous-method-contain-a-yield-statement):

    Good question. I would love to have anonymous iterator blocks. It would be totally awesome to be able to build yourself a little sequence generator in-place that closed over local variables. The reason why not is straightforward: the benefits don't outweigh the costs. The awesomeness of making sequence generators in-place is actually pretty small in the grand scheme of things and nominal methods do the job well enough in most scenarios. So the benefits are not that compelling.

    The costs are large. Iterator rewriting is the most complicated transformation in the compiler, and anonymous method rewriting is the second most complicated. Anonymous methods can be inside other anonymous methods, and anonymous methods can be inside iterator blocks. Therefore, what we do is first we rewrite all anonymous methods so that they become methods of a closure class. This is the second-last thing the compiler does before emitting IL for a method. Once that step is done, the iterator rewriter can assume that there are no anonymous methods in the iterator block; they've all be rewritten already. Therefore the iterator rewriter can just concentrate on rewriting the iterator, without worrying that there might be an unrealized anonymous method in there.

    Also, iterator blocks never "nest", unlike anonymous methods. The iterator rewriter can assume that all iterator blocks are "top level".

    If anonymous methods are allowed to contain iterator blocks, then both those assumptions go out the window. You can have an iterator block that contains an anonymous method that contains an anonymous method that contains an iterator block that contains an anonymous method, and... yuck. Now we have to write a rewriting pass that can handle nested iterator blocks and nested anonymous methods at the same time, merging our two most complicated algorithms into one far more complicated algorithm. It would be really hard to design, implement, and test. We are smart enough to do so, I'm sure. We've got a smart team here. But we don't want to take on that large burden for a "nice to have but not necessary" feature. -- Eric

大致意思就是说,要重写编译器以支持在Lamdba表达式或者匿名方法中进行yield return,工作非常复杂,而且成本很高,远不及其带来的好处。不知道这个解释是什么意思,个人感觉将来还是会支持的。目前来说,其实也不构成太大的不便,只是不能写特别ws的代码罢了。

转载于:https://www.cnblogs.com/Gildor/archive/2009/09/05/1560650.html

相关文章:

  • 通过HTML调用C# [架构]
  • 创建dynamics CRM client-side (五) - 使用regular expression (正则表达式)来检查phone number...
  • C# 中 LISTVIEW用法
  • 【基本数据结构】并查集-C++
  • 如何将数据库从SQL Server迁移到MySQL
  • 回溯算法
  • JS 弹出窗口(DZ论坛)
  • Linux 用epoll实现的简单http服务器
  • Oracle 10g在RHEL6上的另类安装方法
  • 易经读书笔记14火天大有
  • 《Applications=Code+Markup》读书札记(1)——一个简单的 WPF 程序
  • 剑指offer系列25:把数组排成最小的数
  • 分层网络模型
  • 解题报告 『 [USACO07JAN]Balanced Lineup(ST表)』
  • 刚出锅的菜,还热乎呢,要趁热吃哟!
  • Android开源项目规范总结
  • codis proxy处理流程
  • egg(89)--egg之redis的发布和订阅
  • JavaScript 基本功--面试宝典
  • JS实现简单的MVC模式开发小游戏
  • leetcode98. Validate Binary Search Tree
  • linux学习笔记
  • PHP那些事儿
  • Service Worker
  • session共享问题解决方案
  • vue2.0项目引入element-ui
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 理解在java “”i=i++;”所发生的事情
  • 前言-如何学习区块链
  • 如何在GitHub上创建个人博客
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 树莓派用上kodexplorer也能玩成私有网盘
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • #{}和${}的区别是什么 -- java面试
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (5)STL算法之复制
  • (BFS)hdoj2377-Bus Pass
  • (C语言)球球大作战
  • (windows2012共享文件夹和防火墙设置
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (力扣)1314.矩阵区域和
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (转) Face-Resources
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .md即markdown文件的基本常用编写语法
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .NET 读取 JSON格式的数据
  • .Net 垃圾回收机制原理(二)
  • .net 无限分类
  • .net网站发布-允许更新此预编译站点
  • /run/containerd/containerd.sock connect: connection refused
  • [ C++ ] STL_list 使用及其模拟实现