You can use Linq with a helper function
IEnumerable<DateTime> GetDates(DateTime date1, DateTime date2)
{
while (date1 <= date2)
{
yield return date1;
date1 = date1.AddMonths(1);
}
if (date1 > date2)
{
// Include the last month
yield return date1;
}
}
Then MyFunction can be one of the following
1) Include year name
List<string> MyFunction(DateTime date1, DateTime date2) {
return GetDates(date1,date2).Select(x => x.ToString("MMMM yyyyy")).ToList();
}
2) Just the month name, with duplicates
List<string> MyFunction(DateTime date1, DateTime date2) {
return GetDates(date1,date2).Select(x => x.ToString("MMMM")).ToList();
}
3) Distinct month names
List<string> MyFunction(DateTime date1, DateTime date2) {
return GetDates(date1,date2).Select(x => x.ToString("MMMM")).Distinct().ToList();
}