using System;
using System.Collections;
public class SimpleCollection : IEnumerable {
public SimpleCollection(object[] array) {
items = array;
}
public IEnumerator GetEnumerator() {
return new Enumerator(this);
}
private class Enumerator : IEnumerator {
public Enumerator(SimpleCollection obj) {
oThis = obj;
cursor = -1;
}
public bool MoveNext() {
++cursor;
if (cursor > (oThis.items.Length - 1)) {
return false;
}
return true;
}
public void Reset() {
cursor = -1;
}
public object Current {
get {
if (cursor > (oThis.items.Length - 1)) {
throw new InvalidOperationException(
"Enumeration already finished");
}
if (cursor == -1) {
throw new InvalidOperationException(
"Enumeration not started");
}
return oThis.items[cursor];
}
}
private int cursor;
private SimpleCollection oThis;
}
private object[] items = null;
}
Author: coder
yield IEnumerator
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public class Primes {
private long min;
private long max;
public Primes(long minimum, long maximum) {
min = minimum;
max = maximum;
}
public IEnumerator GetEnumerator() {
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++) {
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++) {
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
yield return possiblePrime;
}
}
}
}
class Program {
static void Main(string[] args) {
Primes primesFrom2To1000 = new Primes(2, 1000);
foreach (long i in primesFrom2To1000)
Console.Write("{0} ", i);
}
}
[/csharp]
iterates two collections simultaneously
using System;
using System.Collections.Generic;
public class Starter {
public static void Main() {
MyClass obj = new MyClass();
foreach (int item in obj) {
Console.Write(item);
}
}
}
public class MyClass {
private int[] list1 = new int[] { 0, 2, 4, 6, 8 };
private int[] list2 = new int[] { 1, 3, 5, 7, 9 };
public IEnumerator
for (int index = 0; index < 4; ++index) {
yield return list1[index];
yield return list2[index];
}
}
}
[/csharp]
Tree Enumerator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Tree<TItem> : IEnumerable<TItem> where TItem : IComparable<TItem> {
public Tree(TItem nodeValue) {
this.NodeData = nodeValue;
this.LeftTree = null;
this.RightTree = null;
}
public void Insert(TItem newItem) {
TItem currentNodeValue = this.NodeData;
if (currentNodeValue.CompareTo(newItem) > 0) {
if (this.LeftTree == null) {
this.LeftTree = new Tree<TItem>(newItem);
} else {
this.LeftTree.Insert(newItem);
}
} else {
if (this.RightTree == null) {
this.RightTree = new Tree<TItem>(newItem);
} else {
this.RightTree.Insert(newItem);
}
}
}
public void WalkTree() {
if (this.LeftTree != null) {
this.LeftTree.WalkTree();
}
Console.WriteLine(this.NodeData.ToString());
if (this.RightTree != null) {
this.RightTree.WalkTree();
}
}
public TItem NodeData { get; set; }
public Tree<TItem> LeftTree { get; set; }
public Tree<TItem> RightTree { get; set; }
IEnumerator<TItem> IEnumerable<TItem>.GetEnumerator() {
return new TreeEnumerator<TItem>(this);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
throw new NotImplementedException();
}
}
class TreeEnumerator<T> : IEnumerator<T> where T : IComparable<T> {
public TreeEnumerator(Tree<T> data) {
this.currentData = data;
}
private void populate(Queue<T> enumQueue, Tree<T> tree) {
if (tree.LeftTree != null) {
populate(enumQueue, tree.LeftTree);
}
enumQueue.Enqueue(tree.NodeData);
if (tree.RightTree != null) {
populate(enumQueue, tree.RightTree);
}
}
private Tree<T> currentData = null;
private T currentItem = default(T);
private Queue<T> enumData = null;
T IEnumerator<T>.Current {
get {
if (this.enumData == null)
throw new InvalidOperationException("Use MoveNext before calling Current");
return this.currentItem;
}
}
void IDisposable.Dispose() {
// throw new NotImplementedException();
}
object System.Collections.IEnumerator.Current {
get { throw new NotImplementedException(); }
}
bool System.Collections.IEnumerator.MoveNext() {
if (this.enumData == null) {
this.enumData = new Queue<T>();
populate(this.enumData, this.currentData);
}
if (this.enumData.Count > 0) {
this.currentItem = this.enumData.Dequeue();
return true;
}
return false;
}
void System.Collections.IEnumerator.Reset() {
throw new NotImplementedException();
}
}
class Program {
static void Main(string[] args) {
Tree<int> tree1 = new Tree<int>(10);
tree1.Insert(5);
tree1.Insert(11);
tree1.Insert(5);
tree1.Insert(-12);
tree1.Insert(15);
tree1.Insert(0);
tree1.Insert(14);
tree1.Insert(-8);
tree1.Insert(10);
foreach (int item in tree1)
Console.WriteLine(item);
}
}
Enumerator Example (Versioned Collection)
using System;
using System.Collections;
public class Starter {
public static void Main() {
MyCollection simple = new MyCollection(new object[] { 1, 2, 3, 4, 5, 6, 7 });
IEnumerator enumerator = simple.GetEnumerator();
enumerator.MoveNext();
Console.WriteLine(enumerator.Current);
enumerator.MoveNext();
simple[4] = 10;
Console.WriteLine(enumerator.Current);
enumerator.MoveNext();
}
}
public class MyCollection : IEnumerable {
public MyCollection(object[] array) {
items = array;
version = 1;
}
public object this[int index] {
get {
return items[index];
}
set {
++version;
items[index] = value;
}
}
public IEnumerator GetEnumerator() {
return new Enumerator(this);
}
private class Enumerator : IEnumerator {
public Enumerator(MyCollection obj) {
oThis = obj;
cursor = -1;
version = oThis.version;
}
public bool MoveNext() {
++cursor;
if (cursor > (oThis.items.Length - 1)) {
return false;
}
return true;
}
public void Reset() {
cursor = -1;
}
public object Current {
get {
if (oThis.version != version) {
throw new InvalidOperationException("Collection was modified");
}
if (cursor > (oThis.items.Length - 1)) {
throw new InvalidOperationException("Enumeration already finished");
}
if (cursor == -1) {
throw new InvalidOperationException("Enumeration not started");
}
return oThis.items[cursor];
}
}
private int version;
private int cursor;
private MyCollection oThis;
}
private object[] items = null;
private int version;
}
IEnumerable with a foreach loop
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
IEnumerable<char> query = "Not what you might expect";
foreach (char vowel in "aeiou")
query = query.Where(c => c != vowel);
foreach (char c in query) Console.Write(c);
}
}
Extends IEnumerable
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
public class Employee {
public int currAge;
public string fName, lName;
public Employee() { }
public Employee(string firstName, string lastName, int age) {
currAge = age;
fName = firstName;
lName = lastName;
}
public override string ToString() {
return string.Format("{0}, {1} is {2} years old",
lName, fName, currAge);
}
}
public class PeopleCollection : IEnumerable {
private ArrayList arPeople = new ArrayList();
public PeopleCollection() { }
public Employee GetEmployee(int pos) { return (Employee)arPeople[pos]; }
public void AddEmployee(Employee p) { arPeople.Add(p); }
public void ClearPeople() { arPeople.Clear(); }
public int Count { get { return arPeople.Count; } }
IEnumerator IEnumerable.GetEnumerator() { return arPeople.GetEnumerator(); }
}
class Program {
static void Main(string[] args) {
PeopleCollection myPeople = new PeopleCollection();
myPeople.AddEmployee(new Employee("Homer", "Simpson", 40));
myPeople.AddEmployee(new Employee("Marge", "Simpson", 38));
myPeople.AddEmployee(new Employee("Lisa", "Simpson", 9));
myPeople.AddEmployee(new Employee("Bart", "Simpson", 7));
myPeople.AddEmployee(new Employee("Maggie", "Simpson", 2));
foreach (Employee p in myPeople)
Console.WriteLine(p);
}
}