一些有助于理解“产量".

Some help understanding quot;yieldquot;(一些有助于理解“产量.)
本文介绍了一些有助于理解“产量".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我不断追求减少吸吮的过程中,我试图理解yield"语句,但我一直遇到同样的错误.

In my everlasting quest to suck less I'm trying to understand the "yield" statement, but I keep encountering the same error.

[someMethod] 的主体不能是迭代器块,因为'System.Collections.Generic.List<AClass>' 不是迭代器接口类型.

The body of [someMethod] cannot be an iterator block because 'System.Collections.Generic.List< AClass>' is not an iterator interface type.

这是我卡住的代码:

foreach (XElement header in headersXml.Root.Elements()){
    yield return (ParseHeader(header));                
}

我做错了什么?我不能在迭代器中使用 yield 吗?那有什么意义呢?在此示例中,它表示 List<ProductMixHeader> 不是迭代器接口类型.ProductMixHeader 是一个自定义类,但我想 List 是一个迭代器接口类型,不是吗?

What am I doing wrong? Can't I use yield in an iterator? Then what's the point? In this example it said that List<ProductMixHeader> is not an iterator interface type. ProductMixHeader is a custom class, but I imagine List is an iterator interface type, no?

--编辑--
感谢所有快速回答.
我知道这个问题并不是那么新鲜,并且不断出现相同的资源.
原来我在想我可以返回 List<AClass> 作为返回类型,但由于 List<T> 不是惰性的,它不能.将我的返回类型更改为 IEnumerable<T> 解决了问题:D

--Edit--
Thanks for all the quick answers.
I know this question isn't all that new and the same resources keep popping up.
It turned out I was thinking I could return List<AClass> as a return type, but since List<T> isn't lazy, it cannot. Changing my return type to IEnumerable<T> solved the problem :D

一个有点相关的问题(不值得打开一个新线程):如果我确定 99% 的情况我要去,是否值得将 IEnumerable<T> 作为返回类型去 .ToList() 反正?性能影响是什么?

A somewhat related question (not worth opening a new thread): is it worth giving IEnumerable<T> as a return type if I'm sure that 99% of the cases I'm going to go .ToList() anyway? What will the performance implications be?

推荐答案

使用yield return的方法必须声明为返回以下两个接口之一:

A method using yield return must be declared as returning one of the following two interfaces:

IEnumerable<SomethingAppropriate>
IEnumerator<SomethingApropriate>

(感谢 乔恩 和 Marc 用于指出 IEnumerator)

(thanks Jon and Marc for pointing out IEnumerator)

例子:

public IEnumerable<AClass> YourMethod()
{
    foreach (XElement header in headersXml.Root.Elements())
    {
        yield return (ParseHeader(header));                
    }
}

yield 是一个懒惰的数据生产者,仅在检索到第一个项目后才生成另一个项目,而返回列表将一次性返回所有内容.

yield is a lazy producer of data, only producing another item after the first has been retrieved, whereas returning a list will return everything in one go.

所以有区别,需要正确声明方法.

So there is a difference, and you need to declare the method correctly.

有关更多信息,请阅读 Jon 的答案,其中包含一些非常有用的链接.

For more information, read Jon's answer here, which contains some very useful links.

这篇关于一些有助于理解“产量".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

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,将消息留在队列中)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Specify Publish timeouts in mass transit(指定公共交通中的发布超时)
RabbitMQ asynchronous support(RabbitMQ 异步支持)