Avatar ·

What is the yield keyword in C# for and how does it work?

Hi everyone! I recently started learning C# and came across the `yield` keyword. I read the documentation on MSDN, but I still don't fully understand what it's for. It seems like you could just return a list or an array from a method, so why use yield return? I have this code: I'm writing a method that generates a sequence of Fibonacci numbers. Currently I'm doing it like this: ```csharp public List 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 that you can write the same thing with `yield return`: ```csharp public IEnumerable GetFibonacci(int count) { int a = 0, b = 1; f