Here is my method :
static IEnumerable<DateTime> GetMonths(DateTime from, DateTime to)
{
// if logs is not uptodate
TimeSpan logsMissingTimespan = to - from;
if (logsMissingTimespan != new TimeSpan(0))
{
return GetMonthsBetweenTwoDates(from, to);
}
return null; // Why this line ?
}
private static IEnumerable<DateTime> GetMonthsBetweenTwoDates(DateTime from, DateTime to)
{
DateTime date = from;
DateTime lastDate = DateTime.MaxValue;
while (date < to)
{
if (lastDate.Month != date.Month)
{
lastDate = date;
yield return lastDate;
}
date = date.AddDays(1);
}
}
it works fine but I think I can write something cleaner like this :
static IEnumerable<DateTime> GetMonths(DateTime from, DateTime to)
{
TimeSpan logsMissingTimespan = to - from;
if (logsMissingTimespan == new TimeSpan(0))
{
yield break;
}
return GetMonthsBetweenTwoDates(from, to);
}
But I have an error message :
Cannot return a value from an iterator. Use the yield return statement to return a value, or yield break to end the iteration.
Why should I have a return null and what is the correct syntax ?
EDIT :
So, the correct way is to use Enumerable.Empty :
static IEnumerable<DateTime> GetMonths(DateTime from, DateTime to)
{
// if logs is not uptodate
TimeSpan logsMissingTimespan = to - from;
if (logsMissingTimespan != new TimeSpan(0))
{
return GetMonthsBetweenTwoDates(from, to);
}
return Enumerable.Empty<DateTime>();
}
The forms of your first two examples produce different sorts of output.
Your first example returns an
IEnumerable<T>directly if the condition is satisfied, and a null reference if it is not. Your second example always returns anIEnumerable<T>, but the condition determines whether or not it has any elements.The second example is done by using an iterator block. The
yieldsyntax is used by the C# compiler to turn the function that you wrote into a custom (hidden) type that implementsIEnumerable<T>and a type that implementsIEnumerator<T>. These types implement the necessary state machines in order to achieve (hopefully) the logic that you’ve placed into the function. Because of this, you cannot mix paradigms; you must either return an instance ofIEnumerable<T>from the function (and not useyieldanywhere at all), or everything must be returned viayield.If all you’re concerned with is the fact that you’re returning a null reference, you could make the methods semantically the same by returning
Enumerable.Empty<DateTime>rather thannull.