什么是收益?在ASP .NET中使用收益有什么好处? [英] What is yield and what is the benefit to use yield in asp .NET?

查看:107
本文介绍了什么是收益?在ASP .NET中使用收益有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能帮助我理解asp .NET(C#)中的yield关键字吗?

Can you help me in understanding of yield keyword in asp .NET(C#).

推荐答案

良率返回会自动为您创建一个枚举器.

Yield return automatically creates an enumerator for you.

http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx

所以您可以做类似的事情

So you can do something like

//pseudo code:

while(get_next_record_from_database)
{
  yield return your_next_record;
}

它使您可以快速创建一个对象集合(一个枚举器),您可以循环访问和检索记录. yield return语句处理为您创建枚举器所需的所有代码.

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.

yield return语句的主要部分是,您不必在将集合返回到调用方法之前加载集合中的所有项目.它允许延迟加载,因此您不必一次支付访问费用

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.

何时使用收益回报.

这篇关于什么是收益?在ASP .NET中使用收益有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆