Return IEnumerable by yield

image_pdfimage_print
   
 

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;
        }
    }
}