Материал из .Net Framework эксперт
Simple Iterator
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
public static IEnumerable SimpleList()
{
yield return "string 1";
yield return "string 2";
yield return "string 3";
}
public static void Main(string[] args)
{
foreach (string item in SimpleList())
Console.WriteLine(item);
}
}
Skip the foreach and manually use the enumerable and enumerator for a "yield" IEnumerable
using System;
using System.Collections.Generic;
class LetterCollection
{
string[] letters ={ "A", "B", "C"};
public IEnumerable<string> Forward()
{
for (int i = 0; i < letters.Length; i++)
yield return letters[i];
}
}
class MainClass
{
static void Main()
{
LetterCollection cc = new LetterCollection();
IEnumerable<string> ieable = cc.Forward();
IEnumerator<string> ieator = ieable.GetEnumerator();
while (ieator.MoveNext())
Console.Write("{0} ", ieator.Current);
}
}
A B C
Use multiple yield statements
using System;
using System.Collections;
class MyClass {
public IEnumerator GetEnumerator() {
yield return "A";
yield return "B";
yield return "C";
yield return "D";
yield return "E";
}
}
class MainClass {
public static void Main() {
MyClass mc = new MyClass();
foreach(char ch in mc)
Console.Write(ch + " ");
Console.WriteLine();
}
}
A B C D E
Use yield break
using System;
using System.Collections;
class MyClass {
char ch = "A";
public IEnumerator GetEnumerator() {
for(int i=0; i < 26; i++) {
if(i == 10)
yield break; // stop iterator early
yield return (char) (ch + i);
}
}
}
class MainClass {
public static void Main() {
MyClass mc = new MyClass();
foreach(char ch in mc)
Console.Write(ch + " ");
Console.WriteLine();
}
}
A B C D E F G H I J
Yield Break And Try Finally
using System;
using System.Collections.Generic;
using System.Threading;
using System.ruponentModel;
class MainClass{
static IEnumerable<int> CountWithTimeLimit(DateTime limit){
try{
for (int i = 1; i <= 5; i++){
if (DateTime.Now >= limit){
yield break;
}
yield return i;
}
}finally{
Console.WriteLine("Stopping!");
}
}
static void Main(){
DateTime stop = DateTime.Now.AddSeconds(20);
foreach (int i in CountWithTimeLimit(stop))
{
Console.WriteLine("Received {0}", i);
Thread.Sleep(100);
}
}
}
yield IEnumerable data
using System;
using System.Collections.Generic;
class LetterCollection
{
string[] letters ={ "A", "B", "C"};
public IEnumerable<string> Forward()
{
for (int i = 0; i < letters.Length; i++)
yield return letters[i];
}
}
class MainClass
{
static void Main()
{
LetterCollection cc = new LetterCollection();
foreach (string color in cc.Forward())
Console.Write("{0} ", color);
Console.WriteLine("");
}
}
A B C
Yield with Break
using System;
using System.Collections.Generic;
using System.Threading;
using System.ruponentModel;
class MainClass
{
static IEnumerable<int> CountWithTimeLimit(DateTime limit)
{
for (int i = 1; i <= 5; i++)
{
if (DateTime.Now >= limit)
{
yield break;
}
yield return i;
}
}
static void Main()
{
DateTime stop = DateTime.Now.AddSeconds(2);
foreach (int i in CountWithTimeLimit(stop))
{
Console.WriteLine("Received {0}", i);
Thread.Sleep(100);
}
}
}