I'm manually incrementing an enumerator and passing it to functions to handle subsets of data. Upon return from those functions, I'm finding that the enumerator has not changed state in the parent function.
Here's a simplified version of what I'm talking about.
class Program {
static void Main(string[] args) {
var input = new List<string>() {
"1",
"2",
"escape",
"3",
"4",
"return",
"5"
};
foreach(var line in ParseFile(input)) {
Console.WriteLine(line);
}
Console.ReadLine();
Environment.Exit(Environment.ExitCode);
}
private static IEnumerable<string> ParseFile(List<string> input) {
var enumerator = input.GetEnumerator();
while (enumerator.MoveNext()) {
if (enumerator.Current.Equals("escape")) {
foreach (var line in DoStuff(enumerator)) {
yield return line;
}
} else {
yield return enumerator.Current;
}
}
}
private static IEnumerable<string> DoStuff(IEnumerator<string> enumerator) {
do {
yield return enumerator.Current;
} while (enumerator.MoveNext() && (!enumerator.Current.Equals("return")));
}
}
What I'm seeing is that after exiting DoStuff, enumerator.Current is still "escape" and has not incremented to "return." Here's the results from the console:
1
2
escape
3
4
3
4
return
5
My question is what is causing this to happen? Is a deep copy of the enumerator being passed to the second function? If so, why does that happen? GetHashCode() is the same on both objects.