什么是 Yield?在 ASP .NET 中使用 Yield 有什么好处?
您能帮助我理解 asp .NET(C#)
中的 yield
关键字吗?
Can you help me in understanding of yield
keyword in asp .NET(C#)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
收益回报会自动为您创建一个枚举器。
http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx
因此,您可以执行类似的操作,
它允许您快速创建一个对象集合(枚举器),您可以循环遍历并检索记录。 Yield return 语句处理为您创建枚举器所需的所有代码。
Yield return 语句的重要部分是,在将集合返回到调用方法之前,您不必加载集合中的所有项目。它允许延迟加载集合,因此您不必一次性支付所有访问费用。
何时使用收益回报。
Yield return automatically creates an enumerator for you.
http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx
So you can do something like
It allows you to quickly create an object collection (an Enumerator) that you can loop through and retrieve records. The yield return statement handles all the of the code needed to create an enumerator for you.
The big part of the yield return statement is that you don't have to load all the of the items in a collection before returning the collection to the calling method. It allows lazy loading of the collection, so you don't pay the access penalty all at once.
When to use Yield Return.
产量不仅仅是合成糖或创建 IEnumerables 的简单方法。
有关更多信息,我会查看 Justin Etherage 的博客,其中 有一篇很棒的文章解释了产量的更高级用法。
Yield is much more than syntatic sugar or easy ways to create IEnumerables.
For more information I'd check out Justin Etherage's blog which has a great article explaining more advanced usages of yield.
yield
用作语法糖,从方法返回IEnumerable
或IEnumerator
对象,而无需实现您自己的类实现这些接口。yield
is used as syntactic sugar to return anIEnumerable<T>
orIEnumerator<T>
object from a method without having to implement your own class implementing these interfaces.yield
允许您发出 IEnumerable,您通常会在其中返回更具体的类型(如 IList)。这是一个很好的例子它可以简化您的代码并阐明您的意图。至于您在哪里使用它,在页面上需要迭代集合的任何地方,您都可以使用返回 IEnumerable 代替列表/字典等的方法。
yield
allows you to emit an IEnumerable where you'd normally return a more concrete type (like an IList).This is a pretty good example of how it can simplify your code and clarify your intent. As for where you'd use it, anywhere on your pages that you need to iterate over a collection you could potentially use a method that returns an IEnumerable in place of a List/Dictionary/etc.
我认为使用“停止并继续”模式(又名产量/枚举器)的“好处”没有得到适当的阐述。那么让我试试吧。
假设您有一个应用程序需要从数据库返回 100 万条记录。您有一些常见的做法:
通过使用yield模式,您一次只能将一个对象合并到内存中。此外,对象的消耗由迭代 IEnumerator/IEnumerable 代码的代码控制。这应该是一个典型的 foreach 代码块。
下面是一个例子来对比代码差异
I don't think the "Benefits" of using Stop and Continue pattern (AKA yield/enumerators), has been properly expounded upon. So let me try.
Say you have an app that needs to return 1 million records from the database. You have a few common practices:
By using the yield pattern, you only hydrate one object at a time into memory. Furthermore, the consumption of objects is controlled by the code iterating through the IEnumerator/IEnumerable code. This should be a typical foreach code block.
Here is an example to contrast the code differences