C# 收益率中断有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/497780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:43:07  来源:igfitidea点击:

What's the use of yield break?

c#yielditerator

提问by

Possible Duplicate:
What does “yield break;” do in C#?

可能的重复:
什么是“收益中断”?在 C# 中做什么?

Can anyone see a use for the "yield break" statement that could not have been otherwise achieved by using "break" or "return".

任何人都可以看到使用“中断”或“返回”无法以其他方式实现的“收益中断”语句的用途。

This statement seems to be utterly useless. What's more, without this statement, the "yield return X" statement could have been simplified to "yield X", which much more readable.

这种说法似乎毫无用处。更重要的是,如果没有这个语句,“yield return X”语句可能会被简化为“yield X”,它更具可读性。

What am I missing?

我错过了什么?

回答by Bob

yield break specifies that the method should stop returning results. "return" by itself would not be good enough, or could even lead to a bug, because the return type of the method has to be IEnumerable.

yield break 指定方法应该停止返回结果。“返回”本身不够好,甚至可能导致错误,因为方法的返回类型必须是 IEnumerable。

I am not sure why the return keyword is also needed. My best guess would be that it helps make the intention of the statement a little clearer.

我不确定为什么还需要 return 关键字。我最好的猜测是它有助于使声明的意图更清晰一些。

If you are interested, this article explains what yield is doing behind the scenes

如果你有兴趣,这篇文章解释了 yield 在幕后做了什么

Behind the scenes of the C# yield keyword

C# yield 关键字的幕后花絮

回答by casperOne

You need to have a distinction between returning an IEnumerable implementation that already exists (perhaps you are going to return a List) and an anonymous iterator.

您需要区分返回已经存在的 IEnumerable 实现(也许您将返回一个列表)和匿名迭代器。

You can't just use return or break because those are valid keywords that can be used for other purposes. You also can't use yield on it's own because you need to have a way to specify if you are returning an item in the enumeration, or ending the enumeration, hence the need for yield return and yield break.

您不能只使用 return 或 break,因为它们是可用于其他目的的有效关键字。您也不能单独使用 yield,因为您需要有一种方法来指定是返回枚举中的项目还是结束枚举,因此需要 yield return 和 yield break。

回答by yfeldblum

It is illegal to use a returnstatement in an iterator block. Moreover, the breakstatement could serve a dual purpose within an iterator block if it were to be allowed to affect the iteration: it could be used to break out of a loop or out of a switch, and it could be used to break out of the whole iteration.

return在迭代器块中使用语句是非法的。此外,break如果允许该语句影响迭代,则该语句可以在迭代器块中起到双重作用:它可以用于跳出循环或切换,也可以用于跳出循环整个迭代。

yield returnand yield breakand are two keywords in and of themselves, and they both seem to be necessary.

yield returnandyield break和 and 本身就是两个关键字,它们似乎都是必要的。

回答by chakrit

returnstatement is used to return a value from a function.

return语句用于从函数返回值。

If you have an iterator function like this:

如果您有这样的迭代器函数:

public IEnumerable<int> IteratorOverInt() { }

a return statement in the above function would need to return a filled IEnumerable<int>.

上述函数中的 return 语句需要返回一个填充的IEnumerable<int>.

public IEnumerable<int> IteratorOverInt() {
    return new List<int> { 1, 2, 3, 4 };
}

but if you are working on an iterator function, you will be using yield return, so your code might look like this:

但是如果您正在处理迭代器函数,您将使用yield return,因此您的代码可能如下所示:

public IEnumerable<int> IteratorOverInt() {
    for (int i = 0; i < 4; i++) {
        yield return i;
    }
}

So yield returnis returning an intwhile the method signature demands IEnumerable<int>

所以yield return返回一段int时间方法签名要求IEnumerable<int>

What would a returnstatement do in the above case? Return results overriding the yields? Concatenating whatever to the yields? returndoesn't make much sense in the above case.

return在上述情况下,语句会做什么?返回结果覆盖收益率?将任何东西连接到收益率?return在上述情况下没有多大意义。

Thus we need a way to returnthat make sense in an iterator block, which is why there is yield break.

因此,我们需要一种return在迭代器块中有意义的方法,这就是为什么有yield break.

Of course you can do it with a break, but then how would you exit the function body if you have more code that'd execute? Wrap everything in a complicated if-else block? I'd say just having yield breakwould be better.

当然,您可以使用 a 来完成break,但是如果您有更多要执行的代码,您将如何退出函数体?将所有内容都包装在一个复杂的 if-else 块中?我会说只是有yield break会更好。

回答by Cameron MacFarland

To give a code example, say you want to write an iterator that returns nothing if the source is null or empty.

举一个代码示例,假设您要编写一个迭代器,如果源为 null 或为空,则该迭代器不返回任何内容。

public IEnumerable<T> EnumerateThroughNull<T>(IEnumerable<T> source)
{
    if (source == null)
        yield break;

    foreach (T item in source)
        yield return item;
}

Without the yield break it becomes impossible to return an empty set inside an iterator.

如果没有 yield break,就不可能在迭代器中返回空集。

回答by Ray Hidayat

yield break and break do completely different things! Look

yield break 和 break 做完全不同的事情!看

for(int i = 0; i < 10; ++i) {
   if(i > 5) { break; }
   yield return i;
}

for(int v = 2710; v < 2714; v+=2) {
   yield return v;
}

for(int s = 16; s < 147; ++s) {
   if(s == 27) { yield break; }
   else if(s > 17) { yield return s; }
}

Output would be an IEnumerable of these values: 0, 1, 2, 3, 4, 5, 2710, 2712, 18, 19, 20, 21, 22, 23, 24, 25, 26

输出将是这些值的 IEnumerable:0、1、2、3、4、5、2710、2712、18、19、20、21、22、23、24、25、26

Here's some method which does nothing useful, but it illustrates that you can combine yield break and break into the same method, and they do two different things! Break just breaks out of the loop, yield break ends the method completely.

这是一些没有任何用处的方法,但它说明了您可以将 yield break 和 break 组合到同一个方法中,它们可以做两种不同的事情!Break 只是跳出循环,而 yield break 完全结束了方法。

I guess the reason return isn't used is because you're not returning an IEnumerable yourself in the method. Besides, don't you think yield break is clearer than return, for this kind of iterative situation? I do personally.

我想没有使用 return 的原因是因为您没有在方法中自己返回 IEnumerable 。此外,对于这种迭代情况,您不认为yield break 比return 更清晰吗?我个人做。