Series

Iterator [C#] class Count : IEnumerable<string> - foreach

More Code 2018. 6. 15. 20:05


// C# : Iterator

using System;
using System.Collections;
using System.Collections.Generic;

namespace Iteration
{
class Program
{
static void Main(string[] args)
{
Count c1 = new Count();

foreach (var item in c1)
{
Console.WriteLine(item);
}
}
}

class Count : IEnumerable<string>
{
public IEnumerator<string> GetEnumerator()
{
yield return "one";
yield return "two";
yield return "three";
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}

/* Output
one
two
three
*/