Created
October 16, 2017 16:07
-
-
Save cramhead/b6b3935309a440b0a7b370ba7f38a034 to your computer and use it in GitHub Desktop.
Infinitely interate over array
This file contains hidden or 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
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