You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.0 KiB

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. public static class YieldReturn
  21. {
  22. public static IEnumerable<string> SimpleYieldReturn()
  23. {
  24. yield return "A";
  25. yield return "B";
  26. yield return "C";
  27. }
  28. public static IEnumerable<int> YieldReturnInLoop()
  29. {
  30. for (int i = 0; i < 100; i++) {
  31. yield return i;
  32. }
  33. }
  34. public static IEnumerable<int> YieldReturnWithTryFinally()
  35. {
  36. yield return 0;
  37. try {
  38. yield return 1;
  39. } finally {
  40. Console.WriteLine("Finally!");
  41. }
  42. yield return 2;
  43. }
  44. public static IEnumerable<int> YieldReturnInLock1(object o)
  45. {
  46. lock (o) {
  47. yield return 1;
  48. }
  49. }
  50. public static IEnumerable<int> YieldReturnInLock2(object o)
  51. {
  52. lock (o) {
  53. yield return 1;
  54. o = null;
  55. yield return 2;
  56. }
  57. }
  58. public static IEnumerable<string> YieldReturnWithNestedTryFinally(bool breakInMiddle)
  59. {
  60. Console.WriteLine("Start of method - 1");
  61. yield return "Start of method";
  62. Console.WriteLine("Start of method - 2");
  63. try {
  64. Console.WriteLine("Within outer try - 1");
  65. yield return "Within outer try";
  66. Console.WriteLine("Within outer try - 2");
  67. try {
  68. Console.WriteLine("Within inner try - 1");
  69. yield return "Within inner try";
  70. Console.WriteLine("Within inner try - 2");
  71. if (breakInMiddle)
  72. yield break;
  73. Console.WriteLine("End of inner try - 1");
  74. yield return "End of inner try";
  75. Console.WriteLine("End of inner try - 2");
  76. } finally {
  77. Console.WriteLine("Inner Finally");
  78. }
  79. Console.WriteLine("End of outer try - 1");
  80. yield return "End of outer try";
  81. Console.WriteLine("End of outer try - 2");
  82. } finally {
  83. Console.WriteLine("Outer Finally");
  84. }
  85. Console.WriteLine("End of method - 1");
  86. yield return "End of method";
  87. Console.WriteLine("End of method - 2");
  88. }
  89. public static IEnumerable<string> YieldReturnWithTwoNonNestedFinallyBlocks(IEnumerable<string> input)
  90. {
  91. // outer try-finally block
  92. foreach (string line in input) {
  93. // nested try-finally block
  94. try {
  95. yield return line;
  96. } finally {
  97. Console.WriteLine("Processed " + line);
  98. }
  99. }
  100. yield return "A";
  101. yield return "B";
  102. yield return "C";
  103. yield return "D";
  104. yield return "E";
  105. yield return "F";
  106. // outer try-finally block
  107. foreach (string line in input)
  108. yield return line.ToUpper();
  109. }
  110. public static IEnumerable<Func<string>> YieldReturnWithAnonymousMethods1(IEnumerable<string> input)
  111. {
  112. foreach (string line in input) {
  113. yield return () => line;
  114. }
  115. }
  116. public static IEnumerable<Func<string>> YieldReturnWithAnonymousMethods2(IEnumerable<string> input)
  117. {
  118. foreach (string line in input) {
  119. string copy = line;
  120. yield return () => copy;
  121. }
  122. }
  123. public static IEnumerable<int> GetEvenNumbers(int n)
  124. {
  125. for (int i = 0; i < n; i++) {
  126. if (i % 2 == 0)
  127. yield return i;
  128. }
  129. }
  130. public static IEnumerable<char> YieldChars()
  131. {
  132. yield return 'a';
  133. yield return 'b';
  134. yield return 'c';
  135. }
  136. }