<legend id='1ctCR'><style id='1ctCR'><dir id='1ctCR'><q id='1ctCR'></q></dir></style></legend>
    • <bdo id='1ctCR'></bdo><ul id='1ctCR'></ul>

        <small id='1ctCR'></small><noframes id='1ctCR'>

        <tfoot id='1ctCR'></tfoot>
        <i id='1ctCR'><tr id='1ctCR'><dt id='1ctCR'><q id='1ctCR'><span id='1ctCR'><b id='1ctCR'><form id='1ctCR'><ins id='1ctCR'></ins><ul id='1ctCR'></ul><sub id='1ctCR'></sub></form><legend id='1ctCR'></legend><bdo id='1ctCR'><pre id='1ctCR'><center id='1ctCR'></center></pre></bdo></b><th id='1ctCR'></th></span></q></dt></tr></i><div id='1ctCR'><tfoot id='1ctCR'></tfoot><dl id='1ctCR'><fieldset id='1ctCR'></fieldset></dl></div>
      1. 在 C# 中使用 yield return 迭代器的目的/优势是什么

        时间:2023-08-28
            <bdo id='QEXbJ'></bdo><ul id='QEXbJ'></ul>

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

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

                  <legend id='QEXbJ'><style id='QEXbJ'><dir id='QEXbJ'><q id='QEXbJ'></q></dir></style></legend>
                    <tbody id='QEXbJ'></tbody>
                • 本文介绍了在 C# 中使用 yield return 迭代器的目的/优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我见过的在 C# 方法中使用 yield return x; 的所有示例都可以通过返回整个列表以相同的方式完成.在这些情况下,使用 yield return 语法与返回列表相比有什么好处或优势吗?

                  All of the examples I've seen of using yield return x; inside a C# method could be done in the same way by just returning the whole list. In those cases, is there any benefit or advantage in using the yield return syntax vs. returning the list?

                  另外,yield return 会在哪些类型的场景中使用,而您不能只返回完整列表?

                  Also, in what types of scenarios would yield return be used that you couldn't just return the complete list?

                  推荐答案

                  但是如果你自己建立一个集合呢?

                  But what if you were building a collection yourself?

                  一般来说,迭代器可用于懒惰地生成对象序列.例如 Enumerable.Range 方法内部没有任何类型的集合.它只是按需生成下一个数字.使用状态机生成这种惰性序列有很多用途.其中大部分都包含在函数式编程概念中.

                  In general, iterators can be used to lazily generate a sequence of objects. For example Enumerable.Range method does not have any kind of collection internally. It just generates the next number on demand. There are many uses to this lazy sequence generation using a state machine. Most of them are covered under functional programming concepts.

                  在我看来,如果您将迭代器视为枚举集合的一种方式(它只是最简单的用例之一),那么您就走错了路.正如我所说,迭代器是返回序列的方法.该序列甚至可能是无限.没有办法返回一个无限长的列表并使用前 100 个项目.它不得不有时很懒惰.返回一个集合与返回一个集合生成器有很大的不同(这是一个迭代器).它正在将苹果与橙子进行比较.

                  In my opinion, if you are looking at iterators just as a way to enumerate through a collection (it's just one of the simplest use cases), you're going the wrong way. As I said, iterators are means for returning sequences. The sequence might even be infinite. There would be no way to return a list with infinite length and use the first 100 items. It has to be lazy sometimes. Returning a collection is considerably different from returning a collection generator (which is what an iterator is). It's comparing apples to oranges.

                  假设示例:

                  static IEnumerable<int> GetPrimeNumbers() {
                     for (int num = 2; ; ++num) 
                         if (IsPrime(num))
                             yield return num;
                  }
                  
                  static void Main() { 
                     foreach (var i in GetPrimeNumbers()) 
                         if (i < 10000)
                             Console.WriteLine(i);
                         else
                             break;
                  }
                  

                  此示例打印小于 10000 的素数.您可以轻松地将其更改为打印小于一百万的数字,而无需触及素数生成算法.在此示例中,您无法返回所有素数的列表,因为该序列是无限的,并且消费者甚至不知道从一开始就需要多少项目.

                  This example prints prime numbers less than 10000. You can easily change it to print numbers less than a million without touching the prime number generation algorithm at all. In this example, you can't return a list of all prime numbers because the sequence is infinite and the consumer doesn't even know how many items it wants from the start.

                  这篇关于在 C# 中使用 yield return 迭代器的目的/优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:一些有助于理解“产量". 下一篇:是否可以从第一个使用 foreach 的元素以外的元素

                  相关文章

                  最新文章

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

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

                    <legend id='m0YU5'><style id='m0YU5'><dir id='m0YU5'><q id='m0YU5'></q></dir></style></legend>

                    1. <tfoot id='m0YU5'></tfoot>
                      • <bdo id='m0YU5'></bdo><ul id='m0YU5'></ul>