As far back as during the design for C# 3.0 the design team contemplated a possible language feature to solve the problem of quadratic iterators (the yield foreach syntax) that was never implemented for a variety of reasons and put aside, and while it has been discussed quite a bit by the community on and off it has never gathered enough traction to move forward within the design team. I took it upon myself during my holiday break (2021) to investigate deeper into this problem area and its possible solutions. I present my journey here in my typical rambling fashion.
Iterators help me quicky write methods that represent sequences of values for data that I don’t already have in a single collection, without requiring me to write a complicated state machine in the form an IEnumerator implementation.
I just sprinkle a few yield return
statements into my method and the compiler generates that state machine for me.