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

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

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

    • <bdo id='QDZBD'></bdo><ul id='QDZBD'></ul>

      1. <tfoot id='QDZBD'></tfoot>
        <legend id='QDZBD'><style id='QDZBD'><dir id='QDZBD'><q id='QDZBD'></q></dir></style></legend>

        編寫自定義 IEnumerator&lt;T&gt;帶迭代器

        Writing custom IEnumeratorlt;Tgt; with iterators(編寫自定義 IEnumeratorlt;Tgt;帶迭代器)
          <tbody id='gSvqn'></tbody>

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

              <bdo id='gSvqn'></bdo><ul id='gSvqn'></ul>

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

                <legend id='gSvqn'><style id='gSvqn'><dir id='gSvqn'><q id='gSvqn'></q></dir></style></legend>
                <tfoot id='gSvqn'></tfoot>

                  本文介紹了編寫自定義 IEnumerator&lt;T&gt;帶迭代器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我如何編寫一個自定義的 IEnumerator<T> 實現,它需要維護一些狀態,并且仍然可以使用迭代器塊來簡化它?我能想到的最好的是這樣的:

                  How can I write a custom IEnumerator<T> implementation which needs to maintain some state and still get to use iterator blocks to simplify it? The best I can come up with is something like this:

                  public class MyEnumerator<T> : IEnumerator<T> {
                      private IEnumerator<T> _enumerator;
                      public int Position {get; private set;} // or some other custom properties
                  
                      public MyEnumerator() {
                          Position = 0;
                          _enumerator = MakeEnumerator();
                      }
                  
                      private IEnumerator<T> MakeEnumerator() {
                          // yield return something depending on Position
                      } 
                  
                      public bool MoveNext() {
                          bool res = _enumerator.MoveNext();
                          if (res) Position++;
                          return res;
                      }
                  
                      // delegate Reset and Current to _enumerator as well
                  }
                  
                  public class MyCollection<T> : IEnumerable<T> {
                  
                      IEnumerator<T> IEnumerable<T>.GetEnumerator() {
                          return GetEnumerator();
                      }
                  
                      public MyEnumerator<T> GetEnumerator() {
                          return new MyEnumerator<T>();
                      }
                  
                      ...
                  }
                  

                  推薦答案

                  為什么要寫一個迭代器類?迭代器塊的全部意義在于您不必...

                  Why do you want to write an iterator class? The whole point of an iterator block is so you don't have to...

                  public IEnumerator<T> GetEnumerator() {
                      int position = 0; // state
                      while(whatever) {
                          position++;
                          yield return ...something...;
                      }
                  }
                  

                  如果您添加更多上下文(即,為什么上述內容不起作用),我們可能會提供更多幫助.

                  If you add more context (i,e, why the above can't work), we can probably help more.

                  但如果可能,請避免編寫迭代器類.它們的工作量很大,而且很容易出錯.

                  But if possible, avoid writing an iterator class. They are lots of work, and easy to get wrong.

                  順便說一句,您不必真正為 Reset 煩惱 - 它在很大程度上已被棄用,并且不應該真正使用(因為不能依賴它來為任意枚舉數).

                  By the way, you don't really have to bother with Reset - it is largely deprecated, and shouldn't really ever be used (since it can't be relied to work for an arbitrary enumerator).

                  如果您想使用內部迭代器,那也可以:

                  If you want to consume an inner iterator, that is fine too:

                  int position = 0;
                  foreach(var item in source) {
                     position++;
                     yield return position;
                  }
                  

                  或者如果你只有一個枚舉器:

                  or if you only have an enumerator:

                  while(iter.MoveNext()) {
                     position++;
                     yield return iter.Current;
                  }
                  

                  您也可以考慮將狀態(作為元組)添加到您生成的內容中:

                  You might also consider adding the state (as a tuple) to the thing you yield:

                  class MyState<T> {
                      public int Position {get;private set;}
                      public T Current {get;private set;}
                      public MyState(int position, T current) {...} // assign
                  }
                  ...
                  yield return new MyState<Foo>(position, item);
                  

                  最后,您可以使用 LINQ 樣式的擴展/委托方法,使用 Action<int,T> 向調用者提供位置和值:

                  Finally, you could use a LINQ-style extension/delegate approach, with an Action<int,T> to supply the position and value to the caller:

                      static void Main() {
                          var values = new[] { "a", "b", "c" };
                          values.ForEach((pos, s) => Console.WriteLine("{0}: {1}", pos, s));            
                      }
                      static void ForEach<T>(
                              this IEnumerable<T> source,
                              Action<int, T> action) {
                          if (source == null) throw new ArgumentNullException("source");
                          if (action == null) throw new ArgumentNullException("action");
                  
                          int position = 0;
                          foreach (T item in source) {
                              action(position++, item);
                          }
                      }
                  

                  輸出:

                  0: a
                  1: b
                  2: c
                  

                  這篇關于編寫自定義 IEnumerator&lt;T&gt;帶迭代器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  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)
                  1. <tfoot id='OzKfl'></tfoot>
                      <tbody id='OzKfl'></tbody>
                      <i id='OzKfl'><tr id='OzKfl'><dt id='OzKfl'><q id='OzKfl'><span id='OzKfl'><b id='OzKfl'><form id='OzKfl'><ins id='OzKfl'></ins><ul id='OzKfl'></ul><sub id='OzKfl'></sub></form><legend id='OzKfl'></legend><bdo id='OzKfl'><pre id='OzKfl'><center id='OzKfl'></center></pre></bdo></b><th id='OzKfl'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='OzKfl'><tfoot id='OzKfl'></tfoot><dl id='OzKfl'><fieldset id='OzKfl'></fieldset></dl></div>

                        • <legend id='OzKfl'><style id='OzKfl'><dir id='OzKfl'><q id='OzKfl'></q></dir></style></legend>

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

                            <bdo id='OzKfl'></bdo><ul id='OzKfl'></ul>

                            主站蜘蛛池模板: 精品久久久久久亚洲精品 | 久久五月婷 | 男人的天堂久久 | 久久爱一区 | 精品国产一区二区三区免费 | 奇米久久久 | 欧美日韩成人影院 | 日韩视频―中文字幕 | 午夜私人影院在线观看 | 欧美美乳 | 国产欧美一级 | 精品久久久网站 | 国产精品揄拍一区二区 | 成人黄页在线观看 | 亚洲 欧美 日韩在线 | 亚洲人人 | 亚洲精品欧美 | 精品无码久久久久久久动漫 | 日韩成人在线网址 | 亚洲精品二区 | 在线国产一区二区 | 亚洲一页| 国产一区二区免费电影 | 国产高清视频一区 | 亚洲资源在线 | 精品一区二区三区91 | 日本色婷婷 | 日韩欧美国产一区二区三区 | 欧美精品一二三 | 综合一区二区三区 | www日本在线观看 | 99久久婷婷国产综合精品电影 | av免费在线播放 | 国产精品国产成人国产三级 | 亚洲一区二区三区在线视频 | 欧美日韩视频在线 | 亚洲一区二区三区四区在线观看 | 国产h在线| 午夜精品福利视频 | 91久久精品一区二区三区 | 91欧美精品成人综合在线观看 |