Avatar ·

Proper use of yield return in C#: when and why?

Hello everyone! I recently started learning C# and came across the `yield return` keyword. I read the documentation, but I still don't fully understand in which situations it should be used and in which it shouldn't. For example, I have a method that returns a collection of Fibonacci numbers: ```csharp public static IEnumerable GetFibonacci(int count) { var result = new List(); int a = 0, b = 1; for (int i = 0; i < count; i++) { result.Add(a); int temp = a; a = b; b = temp + b; } return result; } ``` But I've seen some people write it like this: ```csharp public static IEnumerable GetFibonacci(int count) { int a = 0, b = 1; for (int i = 0; i < count; i++) { yield return a; int temp = a; a = ```