Created
October 4, 2011 23:35
-
-
Save einarwh/1263158 to your computer and use it in GitHub Desktop.
Simple number sequence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumberSequence : IEnumerable<int> | |
{ | |
private readonly int _startValue; | |
private readonly int _increment; | |
public NumberSequence(int startValue, int increment) | |
{ | |
_startValue = startValue; | |
_increment = increment; | |
} | |
public IEnumerator<int> GetEnumerator() | |
{ | |
return new NumberEnumerator(_startValue, _increment); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} | |
public class NumberEnumerator : IEnumerator<int>, | |
IComparable<NumberEnumerator> | |
{ | |
private readonly int _increment; | |
private int _currentValue; | |
public NumberEnumerator(int startValue, int increment) | |
{ | |
_currentValue = startValue; | |
_increment = increment; | |
} | |
public bool MoveNext() | |
{ | |
_currentValue += _increment; | |
return true; | |
} | |
public int Current | |
{ | |
get { return _currentValue; } | |
} | |
object IEnumerator.Current | |
{ | |
get { return Current; } | |
} | |
public void Dispose() { } | |
public void Reset() | |
{ | |
throw new NotSupportedException(); | |
} | |
public int CompareTo(NumberEnumerator other) | |
{ | |
return Current.CompareTo(other.Current); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment