yield return 使用.NET的状态机生成器

  • yield return关键词组自动实现IDisposable,使用这个可枚举的地方, 还存在一个隐含的try finally块. 示例代码:
  1. classProgram
  2. {
  3. staticvoidMain(string[] args)
  4. {
  5. foreach(int i inGetEvents())
  6. {
  7. Console.WriteLine(i);
  8. }
  9. Console.ReadLine();
  10. }
  11. staticIEnumerable<int>GetEvents()
  12. {
  13. var integers =new[]{1,2,3,4,5,6,7,8};
  14. foreach(int i in integers)
  15. {
  16. if(i%2==0)
  17. {
  18. yieldreturn i;//返回了一个可迭代对象[2,4,6,8].
  19. }
  20. }
  21. }
  22. }

yield return意味着可以在任何对象上获得可枚举功能, 而无需编写类型化集合类以及相关的处理代码.

  • 一个关于yield returnyield break(在得到需要的遍历之前而中止的过程. 已中序遍历为例:
  1. publicclassNode<T>where T:IComparable<T>
  2. {
  3. public T data;
  4. privateNode<T> leftNode;
  5. publicNode<T>LeftNode{get;set;}
  6. privateNode<T> rightNode;
  7. publicNode<T>RightNode{get;set;}
  8. publicNode(T data)
  9. {
  10. this.data = data;
  11. }
  12. publicoverridestringToString()
  13. {
  14. return data.ToString();
  15. }
  16. }
  17. publicclassBinaryTreeNode<T>where T:IComparable<T>
  18. {
  19. privateNode<T> root;
  20. publicNode<T>Root{get;set;}
  21. publicvoidAdd(T item)
  22. {
  23. if(root ==null)
  24. {
  25. root =newNode<T>(item);
  26. return;
  27. }
  28. Add(item, root);
  29. }
  30. privatevoidAdd(T item,Node<T> node)
  31. {
  32. if(item.CompareTo(node.data)<0)
  33. {
  34. if(node.LeftNode==null)
  35. node.LeftNode=newNode<T>(item);
  36. else
  37. Add(item, node.LeftNode);
  38. }
  39. elseif(item.CompareTo(node.data)>0)
  40. {
  41. if(node.RightNode==null)
  42. node.RightNode=newNode<T>(item);
  43. else
  44. {
  45. Add(item, node.RightNode);
  46. }
  47. }
  48. }
  49. publicvoidAddRange(params T[] items)
  50. {
  51. foreach(var item in items)
  52. {
  53. Add(item);
  54. }
  55. }
  56. publicvoidPrint()
  57. {
  58. Print(root,0,Console.WindowWidth/2);
  59. }
  60. publicIEnumerable<T>Inorder
  61. {
  62. get{returnGetInOrder(this.root);}
  63. }
  64. int depth =0;
  65. /// <summary>
  66. /// 中序遍历: 左 根 右
  67. /// </summary>
  68. /// <param name="node"></param>
  69. /// <returns></returns>
  70. privateIEnumerable<T>GetInOrder(Node<T> node)
  71. {
  72. //if (depth++ > 4) yield break;//大于4层,则只输出前4层.!!!这块待确认.
  73. if(node.LeftNode!=null)
  74. {
  75. foreach(T item inGetInOrder(node.LeftNode))
  76. {
  77. yieldreturn item;
  78. }
  79. }
  80. yieldreturn node.data;
  81. if(node.RightNode!=null)
  82. {
  83. foreach(T item inGetInOrder(node.RightNode))
  84. {
  85. yieldreturn item;
  86. }
  87. }
  88. }
  89. privatevoidPrint(Node<T> item,int depth,int offset)
  90. {
  91. if(item ==null)return;
  92. Console.CursorLeft= offset;
  93. Console.CursorTop= depth;
  94. Console.Write(item.data);
  95. if(item.LeftNode!=null)
  96. Print("/", depth +1, offset -1);
  97. Print(item.LeftNode, depth +2, offset -3);
  98. if(item.RightNode!=null)
  99. Print("\\", depth +1, offset +1);
  100. Print(item.RightNode, depth +2, offset +3);
  101. }
  102. privatevoidPrint(string s,int depth,int offset)
  103. {
  104. Console.CursorLeft= offset;
  105. Console.CursorTop= depth;
  106. Console.Write(s);
  107. }
  108. }
  109. classProgram
  110. {
  111. staticvoidMain(string[] args)
  112. {
  113. BinaryTreeNode<int> integers =newBinaryTreeNode<int>();
  114. integers.AddRange(8,5,6,7,3,4,13,21,1,17);
  115. integers.Print();
  116. Console.ReadLine();
  117. Console.Clear();
  118. foreach(var item in integers.Inorder)
  119. {
  120. Console.WriteLine(item);
  121. }
  122. Console.ReadLine();
  123. }
  124. }

2) LINQ编程技术内幕--yield return的更多相关文章

  1. Java并发编程与技术内幕:线程池深入理解

    摘要: 本文主要讲了Java当中的线程池的使用方法.注意事项及其实现源码实现原理,并辅以实例加以说明,对加深Java线程池的理解有很大的帮助. 首先,讲讲什么是线程池?照笔者的简单理解,其实就是一组线 ...

  2. 《[MySQL技术内幕:SQL编程》读书笔记

    <[MySQL技术内幕:SQL编程>读书笔记 2019年3月31日23:12:11 严禁转载!!! <MySQL技术内幕:SQL编程>这本书是我比较喜欢的一位国内作者姜承尧, ...

  3. 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(下)

    索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 五.透视.逆透视及分组 5.1 透视 所谓透视( ...

  4. 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(上)

    索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 一.SQL Server体系结构 1.1 数据库 ...

  5. 【转】COM技术内幕(笔记)

    COM技术内幕(笔记) COM--到底是什么?--COM标准的要点介绍,它被设计用来解决什么问题?基本元素的定义--COM术语以及这些术语的含义.使用和处理COM对象--如何创建.使用和销毁COM对象 ...

  6. 深入理解linux网络技术内幕读书笔记(三)--用户空间与内核的接口

    Table of Contents 1 概论 1.1 procfs (/proc 文件系统) 1.1.1 编程接口 1.2 sysctl (/proc/sys目录) 1.2.1 编程接口 1.3 sy ...

  7. MFC技术内幕系列之(四)---MFC消息映射与消息传递内幕

    ////////////////////////////////////////////////////////////////////////////////////                 ...

  8. 2008技术内幕:T-SQL语言基础

    2008技术内幕:T-SQL语言基础 单表查询摘记 这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基础>,书中用到的案例数据库是这个 TSQLF ...

  9. 2008技术内幕:T-SQL语言基础 单表查询摘记

    这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基础>,书中用到的案例数据库是这个 TSQLFundamentals2008 ,官网给出的连接是这 ...

随机推荐

  1. 命令删除visualstudio.com云端项目(TFS)

    1.运行命令行 2.tfsdeleteproject /collection:https://域名.visualstudio.com/DefaultCollection “项目名称”

  2. github快速入门(一)

    一.github介绍 git是一款非常知名的代码托管工具.当然现在有了github for windows版本(类似于 svn tortoise). GitHub for Windows 是一个 Me ...

  3. 使用纯代码定义UICollectionView和自定义UICollectionViewCell

    1.自定义UICollectionView 2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollec ...

  4. cdll和windll的差别

    Python要想调用C语言写的动态连接库.不仅要兼容C接口的调用习惯,还须要兼容C语言的数据类型.幸运的是ctypes库已经做了这双方面的工作.以便调用动态连接库是很方便的.在Hello World的 ...

  5. 部署SharePoint解决方式包时遇到的问题

    部署SharePoint解决方式包时遇到的问题 近期我在使用STSADM.EXE命令部署解决方式包的时候.遇到一个问题.很的难搞.         创建WSP文件非常easy.加入到解决方式库也非常e ...

  6. Tomcat 集群

    1.  前言 该篇中测试的机器发生了变更,在第一篇中设置的Apache  DocumentRoot "d:/deployment"修改为了DocumentRoot d:/clust ...

  7. swift 开篇

    苹果的WWDC ,除了公布了os x 10.10 和IOS8 外,还推出了Swift.具体点击这里 代码整体风格有点像Java,也有点像javascript. 以下给出一些代码段(来自苹果官方手冊): ...

  8. 加密解密知识 php非对称加密

    function test1(){ $config = array( "digest_alg" => "sha1", "private_key_ ...

  9. Test complete测试工具介绍

    Test complete 是一款性价比比较高的测试工具,能够满足大多数用户的自动化测试的需求. Test complete 是近几年流行和发展起来的一款自动化测试工具,早期版本由Automated ...

  10. 新闻头条应用源码ios版

    <ignore_js_op>      源码下载:http://code.662p.com/view/13343.html     作者ymcao,源码TopNewsIOS,新闻头条IOS ...