I'm new to C#, have not seen the equivalent of yield
in previous languages I've tried to learn, and am not convinced that it is helpful except perhaps for readability. I survived all these years without it, so why do I need it?
As I undersand, you can use yield return
to spit out values of type T
one-by-one rather than collecting those values into an IEnumerable<T>
and spitting that whole collection out at the end. What's the point? After all, I'm sure there is some overhead involved in interrupting the execution of the function to copy out a single value. Perhaps I'll run some performance tests to see if it's more efficient in terms of time. More than that, I'm wondering if you can show me a specific situation where I would need to iterate through a set of values collected by a function and can only do it with yield
or would be better off doing it with yield
.