Skip to content

Instantly share code, notes, and snippets.

@cramhead
Created October 16, 2017 16:07
Show Gist options
  • Save cramhead/b6b3935309a440b0a7b370ba7f38a034 to your computer and use it in GitHub Desktop.
Save cramhead/b6b3935309a440b0a7b370ba7f38a034 to your computer and use it in GitHub Desktop.
Infinitely interate over array
public class WhatEver {
private int[] _ids;
public WhatEver(string Ids){
_id = appContextIds.Split(',').Select(id => id.Trim()).Select(int.Parse).ToArray();
}
public IEnumerable<int?> NextId()
{
var etor = _ids.GetEnumerator();
while (true)
{
while (etor.MoveNext())
{
if (etor.Current != null)
{
yield return (int)etor.Current;
}
}
etor.Reset();
}
}
}
// In test file
[TestMethod]
public void ProduceARepeatingListOfIds()
{
var results = new List<int>()
{
1234,
567,
8910,
1234,
567,
8910
};
// object holding the NextId method
var b = new WhatEver( "1234, 567, 8910");
var j = 0;
foreach (var i in b.NextId())
{
Assert.AreEqual(results[j], i);
j++;
if (results.Count == j)
{
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment