Why does an IEnumerator have to have at least one yield statement, even if it's unreachable?
- 阅读更多 关于 Why does an IEnumerator have to have at least one yield statement, even if it's unreachable?
问题 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