Why does an IEnumerator have to have at least one yield statement, even if it's unreachable?

给你一囗甜甜゛ 提交于 2019-12-02 00:08:38

问题


Why does this code:

public IEnumerator Test()
{
}

Gives you an error:

Error CS0161 'Test.GetEnumerator()': not all code paths return a value

However this code:

public IEnumerator Test()
{
    if(false)
        yield return 0;
}

Doesn't? (and works as expected; first MoveNext() returns false)

When using IEnumerators as coroutines, sometimes you want to make a coroutine (IEnumerator) that doesn't have an async operations yet (is not yielding anything) but might do that in future.


回答1:


From C# specification:

A block that contains one or more yield statements (§8.14) is called an iterator block. Iterator blocks are used to implement function members as iterators (§10.14).

So if you have one or more yield statements, no matter reachable or not, your method is iterator (under the hood that will generate iterator class). But if you don't have any yield statements your method is the ordinal method (not an iterator) which has a return value of IEnumerable type. As any other method which returns some value, you must either return value of required type or throw an exception from method body. Same rules are applied when you have method which returns string or int value.




回答2:


If there aren't any yield statements in the method block at all, then it's not an iterator block and the compiler has no idea that you want to perform the relevant transformations on it. It's going to treat it just like any other method where you return a value.

The compiler team could have done what they did with async and added a new keyword to the signature of the method, which, if present, made the method an iterator block, allowed yield statements in the body, and would allow an empty body to be treated as yielding nothing, but they chose not to.

If there is a yield statement in the method body somewhere there isn't actually any need for one to reliably be hit for the method to properly compile and run. In an iterator block hitting the end of the method means the sequence is over, even if no items have yet been yielded, which is an entirely sensible behavior. The method still has an IEnumerable or IEnumerator to return, it just has no values to yield.



来源:https://stackoverflow.com/questions/42236881/why-does-an-ienumerator-have-to-have-at-least-one-yield-statement-even-if-its

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!