久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" 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>
    1. 在 C# 中使用 yield return 迭代器的目的/優(yōu)勢是什么

      What is the purpose/advantage of using yield return iterators in C#?(在 C# 中使用 yield return 迭代器的目的/優(yōu)勢是什么?)
      <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'>

                <tbody id='1ctCR'></tbody>

              <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 class="qwawimqqmiuu" id='1ctCR'><tfoot id='1ctCR'></tfoot><dl id='1ctCR'><fieldset id='1ctCR'></fieldset></dl></div>
              • 本文介紹了在 C# 中使用 yield return 迭代器的目的/優(yōu)勢是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我見過的在 C# 方法中使用 yield return x; 的所有示例都可以通過返回整個列表以相同的方式完成.在這些情況下,使用 yield return 語法與返回列表相比有什么好處或優(yōu)勢嗎?

                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 方法內(nèi)部沒有任何類型的集合.它只是按需生成下一個數(shù)字.使用狀態(tài)機生成這種惰性序列有很多用途.其中大部分都包含在函數(shù)式編程概念中.

                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 的素數(shù).您可以輕松地將其更改為打印小于一百萬的數(shù)字,而無需觸及素數(shù)生成算法.在此示例中,您無法返回所有素數(shù)的列表,因為該序列是無限的,并且消費者甚至不知道從一開始就需要多少項目.

                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 迭代器的目的/優(yōu)勢是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                Parse malformed XML(解析格式錯誤的 XML)
                <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 class="qwawimqqmiuu" 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>

                      <tfoot id='m0YU5'></tfoot>
                          <tbody id='m0YU5'></tbody>
                        • <bdo id='m0YU5'></bdo><ul id='m0YU5'></ul>
                        • 主站蜘蛛池模板: 手机看片169 | 免费99精品国产自在在线 | 91婷婷韩国欧美一区二区 | 在线观看黄色电影 | caoporn国产 | 亚洲成人一区二区三区 | 国产一区二区三区不卡av | 国产精品视频二区三区 | 欧美中文字幕一区二区三区亚洲 | 国产网站在线播放 | 91人人澡人人爽 | 久久免费精品视频 | 欧美成年黄网站色视频 | 久草视频在线播放 | 国内av在线| 色婷婷综合久久久中字幕精品久久 | www.久久久久久久久久久久 | 91在线观看视频 | 久久久久久久久久久高潮一区二区 | 国产精品久久久久久 | 国产成人在线一区二区 | 国产精品久久午夜夜伦鲁鲁 | 精品免费视频 | 亚洲精品区 | 97视频久久 | 久久青 | 中文字幕日韩一区二区 | 久久国产高清视频 | 在线视频一区二区 | 国产精品99精品久久免费 | 亚洲精品字幕 | 日日操操| 99久久久99久久国产片鸭王 | 欧美一区二区三区在线免费观看 | 国产激情精品 | 91影院在线观看 | 蜜臀网 | 亚洲精品久久久一区二区三区 | 亚洲黄色在线 | 四虎影视| 国产精品国产a级 |