using System; using System.Collections; using System.Text; class MainEntryPoint { static void Main(string[] args) { Vector Vect1 = new Vector(1.0, 2.0, 5.0); foreach (double Component in Vect1) { foreach (double Next in Vect1) Console.Write(" " + Next); Console.WriteLine(Component); } Console.ReadLine(); } } struct Vector : IFormattable { public double x, y, z; public IEnumerator GetEnumerator() { return new VectorEnumerator(this); } public Vector(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public string ToString(string format, IFormatProvider formatProvider) { if (format == null) return ToString(); string formatUpper = format.ToUpper(); switch (formatUpper) { case "N": return "|| " + Norm().ToString() + " ||"; case "VE": return String.Format("( {0:E}, {1:E}, {2:E} )", x, y, z); case "IJK": StringBuilder sb = new StringBuilder(x.ToString(), 30); sb.Append(" i + "); sb.Append(y.ToString()); sb.Append(" j + "); sb.Append(z.ToString()); sb.Append(" k"); return sb.ToString(); default: return ToString(); } } public Vector(Vector rhs) { x = rhs.x; y = rhs.y; z = rhs.z; } public override string ToString() { return "( " + x + " , " + y + " , " + z + " )"; } public double this[uint i] { get { switch (i) { case 0: return x; case 1: return y; case 2: return z; default: throw new IndexOutOfRangeException( "Attempt to retrieve Vector element" + i); } } set { switch (i) { case 0: x = value; break; case 1: y = value; break; case 2: z = value; break; default: throw new IndexOutOfRangeException( "Attempt to set Vector element" + i); } } } private const double Epsilon = 0.0000001; public static bool operator ==(Vector lhs, Vector rhs) { if (System.Math.Abs(lhs.x - rhs.x) < Epsilon && System.Math.Abs(lhs.y - rhs.y) < Epsilon && System.Math.Abs(lhs.z - rhs.z) < Epsilon) return true; else return false; } public static bool operator !=(Vector lhs, Vector rhs) { return !(lhs == rhs); } public static Vector operator +(Vector lhs, Vector rhs) { Vector Result = new Vector(lhs); Result.x += rhs.x; Result.y += rhs.y; Result.z += rhs.z; return Result; } public static Vector operator *(double lhs, Vector rhs) { return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z); } public static Vector operator *(Vector lhs, double rhs) { return rhs * lhs; } public static double operator *(Vector lhs, Vector rhs) { return lhs.x * rhs.x + lhs.y + rhs.y + lhs.z * rhs.z; } public double Norm() { return x * x + y * y + z * z; } private class VectorEnumerator : IEnumerator { Vector theVector; int location; public VectorEnumerator(Vector theVector) { this.theVector = theVector; location = -1; } public bool MoveNext() { ++location; return (location > 2) ? false : true; } public object Current { get { if (location < 0 || location > 2) throw new InvalidOperationException( "The enumerator is either before the first element or " + "after the last element of the Vector"); return theVector[(uint)location]; } } public void Reset() { location = -1; } } }
Class Interface
Collection is iterated using the IEnumerator interface
using System; using System.Collections; public class Starter { public static void Main() { SimpleCollection simple = new SimpleCollection(new object[] { 1, 2, 3, 4, 5, 6, 7 }); IEnumerator enumerator = simple.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); } } } public class SimpleCollection : IEnumerable { public SimpleCollection(object[] array) { items = array; } public IEnumerator GetEnumerator() { return new Enumerator(items); } private object[] items = null; } public class Enumerator : IEnumerator { public Enumerator(object[] items) { elements = new object[items.Length]; Array.Copy(items, elements, items.Length); cursor = -1; } public bool MoveNext() { ++cursor; if (cursor > (elements.Length - 1)) { return false; } return true; } public void Reset() { cursor = -1; } public object Current { get { if (cursor > (elements.Length - 1)) { throw new InvalidOperationException("Enumeration already finished"); } if (cursor == -1) { throw new InvalidOperationException( "Enumeration not started"); } return elements[cursor]; } } private int cursor; private object[] elements = null; }
IEnumerator Example (Static Collection)
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; }
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; }