Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save soheil-moonesi/2f4f301a41d5c145b5d37e0d5623fdfc to your computer and use it in GitHub Desktop.

Select an option

Save soheil-moonesi/2f4f301a41d5c145b5d37e0d5623fdfc to your computer and use it in GitHub Desktop.
class OpenRestaurantsEnumerator : IEnumerator
{
private readonly List<Restaurant> _restaurants;
private int _position = -1;
public OpenRestaurantsEnumerator(List<Restaurant> restaurants)
{
_restaurants = restaurants;
}
public object Current
{
get
{
if (_position == -1 || _position >= _restaurants.Count)
throw new InvalidOperationException();
return _restaurants[_position];
}
}
public bool MoveNext()
{
while(_position < _restaurants.Count)
{
_position++;
if (_position < _restaurants.Count && _restaurants[_position].IsOpened)
{
return true;
}
}
return false;
}
public void Reset()
{
_position = -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment