Yield
Inroduction:
yield keyword is used to return items from a loop within a method and retain the state of the method through multiple calls.
Microsoft
When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. You use an iterator to perform a custom iteration over a collection. The following example shows the two forms of the yield statement.
yield return <expression>; yield break;
Sample Code:
Each time Yield is called, it will return one value in the given range and it maintains the state between calls. The best way to think of it is that for the first call to the method, execution starts at the first line and continues until it hits a yield statement at which time it returns the value. The subsequent runs through the method continue execution at the statement after the yield, continuing to yield values or exiting at the end of the method.
Output:
yield keyword is used to return items from a loop within a method and retain the state of the method through multiple calls.
Microsoft
When you use the yield keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator. You use an iterator to perform a custom iteration over a collection. The following example shows the two forms of the yield statement.
yield return <expression>; yield break;
Sample Code:
Project Type: CONSOLE
Project Name: Yield
/* Class Yield.cs */
using System; using System.Collections; using System.Collections.Generic; using System.Threading; namespace Yield { class Yield { public static IEnumerable Prime { get { Console.WriteLine(String.Format("{0}{1} Example Start{2}", new String('*', 5), "Prime", new String('*', 5))); int number = 1; while (number <= 100) { int i = 2; while (i <= number) { if (number % i == 0) break; i++; } if (i == number) yield return i; number++; } Console.WriteLine(String.Format("{0}{1} Example End{2}", new String('*', 5), "Prime", new String('*', 5))); } } public static IEnumerable PrimeWithPause { get { Console.WriteLine(String.Format("{0}{1} Example Start{2}", new String('*', 5), "Prime With Pause", new String('*', 5))); int number = 1; while (number <= 100) { int i = 2; while (i <= number) { if (number % i == 0) break; i++; } if (i == number) { if (i > 70) { Console.WriteLine("I am sleeping for 20 seconds..."); Thread.Sleep(new TimeSpan(0, 0, 0, 20)); } yield return i; } number++; } Console.WriteLine(String.Format("{0}{1} Example End{2}", new String('*', 5), "Prime With Pause", new String('*', 5))); } } public static IEnumerable PrimeWithPauseAndNoYield { get { Console.WriteLine(String.Format("{0}{1} Example Start{2}", new String('*', 5), "Prime With Pause No Yield", new String('*', 5))); var intList = new List<int>(); int number = 1; while (number <= 100) { int i = 2; while (i <= number) { if (number % i == 0) break; i++; } if (i == number) { if (i > 70) { Console.WriteLine("I am sleeping for 5 sec..."); Thread.Sleep(new TimeSpan(0, 0, 0, 5)); } intList.Add(i); } number++; } Console.WriteLine(String.Format("{0}{1} Example End{2}", new String('*', 5), "Prime With Pause No Yield", new String('*', 5))); return intList; } } public static IEnumerable<int> PrimeRangeYield(int start, int end) { Console.WriteLine(String.Format("{0}{1} Example Start{2}", new String('*', 5), "Prime Range", new String('*', 5))); Console.WriteLine(string.Format("start:{0} end:{1}", start, end)); int number = 1; while (number <= 100) { int i = 2; while (i <= number) { if (number % i == 0) break; i++; } if (i == number) { if (i >= start && i < end) { yield return i; } } number++; } Console.WriteLine(String.Format("{0}{1} Example End{2}", new String('*', 5), "Prime Range", new String('*', 5))); } public static IEnumerable PrimeBrakeYield { get { Console.WriteLine(String.Format("{0}{1} Example Start{2}", new String('*', 5), "Prime Brake", new String('*', 5))); int number = 1; while (number <= 100) { int i = 2; while (i <= number) { if (number % i == 0) break; i++; } if (i == number){ if (i > 70) { Console.WriteLine(String.Format("{0}{1} Example End{2}", new String('*', 5), "Prime Brake", new String('*', 5))); yield break; } yield return i; } number++;} } } } }
Explanation:/* Class Program.cs */ using System; namespace Yield { class Program { static void Main() { Console.WriteLine("We are testing yield with: "); Console.WriteLine("1. Prime Example"); Console.WriteLine("2. Prime Range Example"); Console.WriteLine("3. Prime Paused Example"); Console.WriteLine("4. Prime Break Example"); Console.Write("Please input your test number "); var choice = Console.ReadLine(); while (string.IsNullOrWhiteSpace(choice) || string.IsNullOrEmpty(choice)) { Console.WriteLine("Wrong input, please try again"); choice = Console.ReadLine(); } if (choice == "1") { foreach (int i in Yield.Prime) Console.WriteLine(i); Console.ReadLine(); } else if (choice == "2") { foreach (int i in Yield.PrimeRangeYield(20, 50)) Console.WriteLine(i); Console.ReadLine(); } else if (choice == "3") { foreach (int i in Yield.PrimeWithPauseAndNoYield) Console.WriteLine(i); foreach (int i in Yield.PrimeWithPause) Console.WriteLine(i); Console.ReadLine(); } else if (choice == "4") { foreach (int i in Yield.PrimeBrakeYield) Console.WriteLine(i); Console.ReadLine(); } else { Console.WriteLine("*******Invalid choice********"); Console.ReadLine(); } } } }
Each time Yield is called, it will return one value in the given range and it maintains the state between calls. The best way to think of it is that for the first call to the method, execution starts at the first line and continues until it hits a yield statement at which time it returns the value. The subsequent runs through the method continue execution at the statement after the yield, continuing to yield values or exiting at the end of the method.
Output:
Prime Example:
We are testing yield with:1. Prime Example 2. Prime Range Example 3. Prime Paused Example 4. Prime Break Example Please input your test number 1 *****Prime Example Start***** 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 *****Prime Example End*****
Prime Range Example:
We are testing yield with: 1. Prime Example 2. Prime Range Example 3. Prime Paused Example 4. Prime Break Example Please input your test number 2 *****Prime Range Example Start***** start:20 end:50 23 29 31 37 41 43 47 *****Prime Range Example End*****
Prime Paused Example:
We are testing yield with:1. Prime Example 2. Prime Range Example 3. Prime Paused Example 4. Prime Break Example Please input your test number 3 *****Prime With Pause No Yield Example Start***** I am sleeping for 2 sec... I am sleeping for 2 sec... I am sleeping for 2 sec... I am sleeping for 2 sec... I am sleeping for 2 sec... I am sleeping for 2 sec... *****Prime With Pause No Yield Example End***** 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 *****Prime With Pause Example Start***** 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 I am sleeping for 2 seconds... 71 I am sleeping for 2 seconds... 73 I am sleeping for 2 seconds... 79 I am sleeping for 2 seconds... 83 I am sleeping for 2 seconds... 89 I am sleeping for 2 seconds... 97 *****Prime With Pause Example End*****We are testing yield with: 1. Prime Example 2. Prime Range Example 3. Prime Paused Example 4. Prime Break Example Please input your test number 4 *****Prime Brake Example Start***** 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 *****Prime Brake Example End*****Prime Brake Example:
Comments
Post a Comment