Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created October 4, 2011 23:35
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 einarwh/1263158 to your computer and use it in GitHub Desktop.
Save einarwh/1263158 to your computer and use it in GitHub Desktop.
Simple number sequence.
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