Return IEnumerable by yield : IEnumerable « Class Interface « C# / C Sharp






Return IEnumerable by yield

 

using System;
using System.Collections;

public class Starter {
    public static void Main() {
        foreach (string day in ToEndOfMonth()) {
            Console.WriteLine(day);
        }
    }

    public static IEnumerable ToEndOfMonth() {
        DateTime date = DateTime.Now;

        int currMonth = date.Month;
        while (currMonth == date.Month) {
            string temp = currMonth.ToString() + "/" + date.Day.ToString();
            date = date.AddDays(1);
            yield return temp;
        }
    }
}

 








Related examples in the same category

1.yield return
2.return IEnumerable
3.iterates a collection forward and reverse
4.Extends IEnumerable
5.IEnumerable with a foreach loop
6.Enumerator Example (Versioned Collection)
7.Tree Enumerator