Last active
December 24, 2015 01:29
-
-
Save flarn2006/6724355 to your computer and use it in GitHub Desktop.
Just a simple class that implements IEnumerable.
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
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