Using IEnumerable without foreach loop
c#, ienumerable, yield-return
Solution
You can get a reference to the `Enumerator`, using the `GetEnumerator` method, then you can use the `MoveNext()` method to move on, and use the `Current` property to access your elements:
var enumerator = getInt().GetEnumerator();
while(enumerator.MoveNext())
{
int n = enumerator.Current;
Console.WriteLine(n);
}
Problem
I've gotta be missing something simple here. Take the following code: ``` public IEnumerable<int> getInt(){ for(int i = 0; i < 10; i++){ yield return i; } } ``` I can call this with: ``` foreach (int j in obj.getInt()){ //do something with j } ``` How can I use the getInt method without the foreach loop: ``` IEnumerable<int> iter = obj.getInt(); // do something with iter ?? ``` Thanks. EDITS For those wondering why I'd want this. I'm iterating two things: ``` IEnumerator<int> iter = obj.getInt().GetEnumerator(); foreach(object x in xs){ if (x.someCondition) continue; iter.MoveNext(); int n = iter.current(); x.someProp = n; etc... } ```