开发者

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 returns 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜