using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace IntervalAttempt { public class Program { static void Main(string[] args) { Int32IntervalI i1 = new Int32IntervalI(0, 100, 10); while (i1.hasNext()) { Console.WriteLine(i1.next()); } Int32IntervalD i2 = new Int32IntervalD(0, 100, 10); while (i2.hasNext()) { Console.WriteLine(i2.next()); } } } #region Interface Version public interface IBinaryAddition { V Add(V left, V right); } public interface IBinaryLessThanOrEqual { bool LessThan(V left, V right); } public class Int32IntervalI : IntervalWithInterface { #region Operators private class Int32BinaryAddition : IBinaryAddition { public int Add(int left, int right) { return left + right; } } private class Int32BinaryLessThanOrEqual : IBinaryLessThanOrEqual { public bool LessThan(int left, int right) { return left <= right; } } #endregion private static Int32BinaryAddition adder = new Int32BinaryAddition(); private static Int32BinaryLessThanOrEqual comparer = new Int32BinaryLessThanOrEqual(); public Int32IntervalI(int start, int stop, int step) : base(start, stop, step, adder, comparer) { } } public class IntervalWithInterface { T start; T current; T stop; T step; IBinaryAddition adder; IBinaryLessThanOrEqual comparer; public IntervalWithInterface(T start, T stop, T step, IBinaryAddition adder, IBinaryLessThanOrEqual comparer) { this.start = start; this.current = start; this.stop = stop; this.step = step; this.adder = adder; this.comparer = comparer; } public bool hasNext() { return comparer.LessThan(current, stop); } public T next() { T tmp = current; if (hasNext()) { current = adder.Add(current, step); } return tmp; } } #endregion #region Delegate Version public delegate V BinaryAddition(V left, V right); public delegate bool BinaryLessThanOrEqual(V left, V right); public class Int32IntervalD : IntervalWithDelegate { private static BinaryAddition adder = new BinaryAddition(delegate(int left, int right) { return left + right; }); private static BinaryLessThanOrEqual comparer = new BinaryLessThanOrEqual(delegate(int left, int right) { return left <= right; }); public Int32IntervalD(int start, int stop, int step) : base(start, stop, step, adder, comparer) { } } public class IntervalWithDelegate { T start; T current; T stop; T step; BinaryAddition addBlock; BinaryLessThanOrEqual lessThanBlock; public IntervalWithDelegate(T start, T stop, T step, BinaryAddition addBlock, BinaryLessThanOrEqual lessThanBlock) { this.start = start; this.current = start; this.stop = stop; this.step = step; this.addBlock = addBlock; this.lessThanBlock = lessThanBlock; } public bool hasNext() { return lessThanBlock(current, stop); } public T next() { T tmp = current; if (hasNext()) { current = addBlock(current, step); } return tmp; } } #endregion }