<bdo id='5S1zg'></bdo><ul id='5S1zg'></ul>

    1. <tfoot id='5S1zg'></tfoot>

    2. <legend id='5S1zg'><style id='5S1zg'><dir id='5S1zg'><q id='5S1zg'></q></dir></style></legend>

      <small id='5S1zg'></small><noframes id='5S1zg'>

      <i id='5S1zg'><tr id='5S1zg'><dt id='5S1zg'><q id='5S1zg'><span id='5S1zg'><b id='5S1zg'><form id='5S1zg'><ins id='5S1zg'></ins><ul id='5S1zg'></ul><sub id='5S1zg'></sub></form><legend id='5S1zg'></legend><bdo id='5S1zg'><pre id='5S1zg'><center id='5S1zg'></center></pre></bdo></b><th id='5S1zg'></th></span></q></dt></tr></i><div id='5S1zg'><tfoot id='5S1zg'></tfoot><dl id='5S1zg'><fieldset id='5S1zg'></fieldset></dl></div>
    3. 一些有助于理解“产量".

      时间:2023-08-28
        <tbody id='ozm4r'></tbody>

        1. <i id='ozm4r'><tr id='ozm4r'><dt id='ozm4r'><q id='ozm4r'><span id='ozm4r'><b id='ozm4r'><form id='ozm4r'><ins id='ozm4r'></ins><ul id='ozm4r'></ul><sub id='ozm4r'></sub></form><legend id='ozm4r'></legend><bdo id='ozm4r'><pre id='ozm4r'><center id='ozm4r'></center></pre></bdo></b><th id='ozm4r'></th></span></q></dt></tr></i><div id='ozm4r'><tfoot id='ozm4r'></tfoot><dl id='ozm4r'><fieldset id='ozm4r'></fieldset></dl></div>
        2. <legend id='ozm4r'><style id='ozm4r'><dir id='ozm4r'><q id='ozm4r'></q></dir></style></legend>

            <tfoot id='ozm4r'></tfoot>
            • <bdo id='ozm4r'></bdo><ul id='ozm4r'></ul>

              <small id='ozm4r'></small><noframes id='ozm4r'>

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

                问题描述

                在我不断追求减少吸吮的过程中,我试图理解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.

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

                上一篇:异步迭代器任务&lt;IEnumerable&lt;T&gt;& 下一篇:在 C# 中使用 yield return 迭代器的目的/优势是什么

                相关文章

                最新文章

              • <tfoot id='bOwua'></tfoot>
                  <bdo id='bOwua'></bdo><ul id='bOwua'></ul>

                1. <small id='bOwua'></small><noframes id='bOwua'>

                2. <legend id='bOwua'><style id='bOwua'><dir id='bOwua'><q id='bOwua'></q></dir></style></legend>

                    <i id='bOwua'><tr id='bOwua'><dt id='bOwua'><q id='bOwua'><span id='bOwua'><b id='bOwua'><form id='bOwua'><ins id='bOwua'></ins><ul id='bOwua'></ul><sub id='bOwua'></sub></form><legend id='bOwua'></legend><bdo id='bOwua'><pre id='bOwua'><center id='bOwua'></center></pre></bdo></b><th id='bOwua'></th></span></q></dt></tr></i><div id='bOwua'><tfoot id='bOwua'></tfoot><dl id='bOwua'><fieldset id='bOwua'></fieldset></dl></div>