Answers for "c# yield"

C#
3

c# yield

using System;
using System.Collections.Generic;

public class YieldSample
{
    static void Main()
    {
        foreach (var number in GenerateWithoutYield())
            Console.WriteLine(number);
    }
    public static IEnumerable<int> GenerateWithoutYield()
    {
        var i = 0;
        var list = new List<int>();
      
        while (i < 5)  // yield gives the control to this part and will hit only this part everytime we call GenerateWithoutYield
            yield return ++i;

    }
}
Posted by: Guest on May-18-2020

C# Answers by Framework

Browse Popular Code Answers by Language