in a method which produces lazy list, what happens if i return nothing?
it seems this code is valid. I am wondering what 开发者_高级运维if getTest(1)
IEnumerable<int> getTest(int n)
{
if (n == 0)
yield return 1;
else
;
}
It will return an empty but non-null IEnumberable<int>
.
Once you've marked a method as an iterator (by using yield
anywhere in the method), it can execute zero or more yield return
s to return data in the sequence.
In fact, the simplest way to get an empty IEnumberable is IEnumerable<T> Empty() { yield break; }
.
Or just call Enumerable.Empty<T>()
.
It just returns an empty, although fully "valid", enumerable. Everything went exactly as expected.
精彩评论