Skip to content

Instantly share code, notes, and snippets.

@miteshsureja
Created June 11, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miteshsureja/d66004de5029d9d590818baf565bf464 to your computer and use it in GitHub Desktop.
Save miteshsureja/d66004de5029d9d590818baf565bf464 to your computer and use it in GitHub Desktop.
Iterator Pattern using IEnumerable example
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
public class MyCollection : IEnumerable
{
List<int> myList = new List<int>() { 2, 5, 3, 6, 4, 9 };
public IEnumerator GetEnumerator()
{
foreach (var item in myList)
yield return item;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
class Program
{
static void Main(string[] args)
{
MyCollection coll = new MyCollection();
foreach(var item in coll)
Console.WriteLine(item.ToString());
Console.ReadLine();
}
}
}
@miteshsureja
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment