yield 语句实现

yield statement implementation(yield 语句实现)
本文介绍了yield 语句实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想以易于理解的形式了解有关 yield 语句的所有信息.

I want to know everything about the yield statement, in an easy to understand form.

我已经阅读了 yield 语句及其在实现迭代器模式时的易用性.但是,大部分都非常干燥.我想深入了解一下微软是如何处理收益率的.

I have read about the yield statement and its ease when implementing the iterator pattern. However, most of it is very dry. I would like to get under the covers and see how Microsoft handles return yield.

另外,你什么时候使用yield break?

Also, when do you use yield break?

推荐答案

yield 通过在内部构建状态机来工作.它会在例程退出时存储该例程的当前状态,并在下次从该状态恢复时存储.

yield works by building a state machine internally. It stores the current state of the routine when it exits and resumes from that state next time.

您可以使用 Reflector 查看编译器是如何实现的.

You can use Reflector to see how it's implemented by the compiler.

yield break 当你想停止返回结果时使用.如果您没有 yield break,编译器会在函数末尾假定一个(就像普通函数中的 return; 语句一样)

yield break is used when you want to stop returning results. If you don't have a yield break, the compiler would assume one at the end of the function (just like a return; statement in a normal function)

这篇关于yield 语句实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
How to generate password_hash for RabbitMQ Management HTTP API(如何为 RabbitMQ 管理 HTTP API 生成密码哈希)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Wait for a single RabbitMQ message with a timeout(等待一条带有超时的 RabbitMQ 消息)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Specify Publish timeouts in mass transit(指定公共交通中的发布超时)