本作业题是列出本月所有星期天的日期。
public IEnumerable<DateTime> AllSundaysInMonth(int year, int month)
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
int days = ci.Calendar.GetDaysInMonth(year, month);
for (int i = 1; i <= days; i++)
{
if (new DateTime(year, month, i).DayOfWeek == DayOfWeek.Sunday)
yield return new DateTime(year, month, i);
}
}
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
int days = ci.Calendar.GetDaysInMonth(year, month);
for (int i = 1; i <= days; i++)
{
if (new DateTime(year, month, i).DayOfWeek == DayOfWeek.Sunday)
yield return new DateTime(year, month, i);
}
}