Skip to content

Instantly share code, notes, and snippets.

@flarn2006
Last active December 24, 2015 01:29
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 flarn2006/6724355 to your computer and use it in GitHub Desktop.
Save flarn2006/6724355 to your computer and use it in GitHub Desktop.
Just a simple class that implements IEnumerable.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YourNamespaceHere // this sucks for copying classes between projects
{
public class Counter : IEnumerable<int>
{
private int min, max, step;
public Counter(int min, int max)
{
this.min = min;
this.max = max;
this.step = (min > max) ? -1 : 1;
}
public Counter(int min, int max, int step)
{
this.min = min;
this.max = max;
this.step = step;
}
public IEnumerator<int> GetEnumerator()
{
return new Enumerator(min, max, step);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public class Enumerator : IEnumerator<int>
{
private int min, max, step;
private int value;
public Enumerator(int min, int max, int step)
{
this.min = min;
this.max = max;
this.step = step;
Reset();
}
public int Current
{
get { return value; }
}
object System.Collections.IEnumerator.Current
{
get { return Current; }
}
public bool MoveNext()
{
value += step;
return (value <= max);
}
public void Reset()
{
value = min - step;
}
public void Dispose()
{
// nothing to do here
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment