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

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

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

        在 lambda 表達(dá)式中使用 foreach 循環(huán)的迭代器變量

        Using the iterator variable of foreach loop in a lambda expression - why fails?(在 lambda 表達(dá)式中使用 foreach 循環(huán)的迭代器變量 - 為什么會失敗?)
        • <small id='UeLqU'></small><noframes id='UeLqU'>

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

            • <bdo id='UeLqU'></bdo><ul id='UeLqU'></ul>
                <tbody id='UeLqU'></tbody>
              <tfoot id='UeLqU'></tfoot>
            • <legend id='UeLqU'><style id='UeLqU'><dir id='UeLqU'><q id='UeLqU'></q></dir></style></legend>
                  本文介紹了在 lambda 表達(dá)式中使用 foreach 循環(huán)的迭代器變量 - 為什么會失敗?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  考慮以下代碼:

                  公共類 MyClass{公共委托字符串 PrintHelloType(字符串問候語);公共無效執(zhí)行(){Type[] types = new Type[] { typeof(string), typeof(float), typeof(int)};列表<PrintHelloType>helloMethods = 新列表<PrintHelloType>();foreach(類型中的 var 類型){var sayHello =new PrintHelloType(greeting => SayGreetingToType(type, greeting));helloMethods.Add(sayHello);}foreach(helloMethods 中的 var helloMethod){Console.WriteLine(helloMethod("Hi"));}}public string SayGreetingToType(Type type, string greetingText){返回 greetingText + " " + type.Name;}...}

                  調(diào)用myClass.Execute()后,代碼打印出以下意外響應(yīng):

                  <上一頁>嗨 Int32嗨 Int32嗨 Int32

                  顯然,我希望 "Hi String""Hi Single""Hi Int32",但顯然不是案件.為什么在所有 3 種方法中都使用迭代數(shù)組的最后一個元素而不是適當(dāng)?shù)姆椒?

                  您將如何重寫代碼以實(shí)現(xiàn)預(yù)期目標(biāo)?

                  解決方案

                  歡迎來到閉包和捕獲變量的世界:)

                  Eric Lippert 對此行為有深入的解釋:

                  • 結(jié)束在被認(rèn)為有害的循環(huán)變量上
                  • 結(jié)束在循環(huán)變量上,第二部分

                  基本上,捕獲的是循環(huán)變量,而不是值.要獲得您認(rèn)為應(yīng)該獲得的東西,請執(zhí)行以下操作:

                  foreach (var type in types){var newType = 類型;var sayHello =new PrintHelloType(greeting => SayGreetingToType(newType, greeting));helloMethods.Add(sayHello);}

                  Consider the following code:

                  public class MyClass
                  {
                     public delegate string PrintHelloType(string greeting);
                  
                  
                      public void Execute()
                      {
                  
                          Type[] types = new Type[] { typeof(string), typeof(float), typeof(int)};
                          List<PrintHelloType> helloMethods = new List<PrintHelloType>();
                  
                          foreach (var type in types)
                          {
                              var sayHello = 
                                  new PrintHelloType(greeting => SayGreetingToType(type, greeting));
                              helloMethods.Add(sayHello);
                          }
                  
                          foreach (var helloMethod in helloMethods)
                          {
                              Console.WriteLine(helloMethod("Hi"));
                          }
                  
                      }
                  
                      public string SayGreetingToType(Type type, string greetingText)
                      {
                          return greetingText + " " + type.Name;
                      }
                  
                  ...
                  
                  }
                  

                  After calling myClass.Execute(), the code prints the following unexpected response:

                  Hi Int32
                  Hi Int32
                  Hi Int32  
                  

                  Obviously, I would expect "Hi String", "Hi Single", "Hi Int32", but apparently it is not the case. Why the last element of the iterated array is being used in all the 3 methods instead of the appropriate one?

                  How would you rewrite the code to achieve the desired goal?

                  解決方案

                  Welcome to the world of closures and captured variables :)

                  Eric Lippert has an in-depth explanation of this behaviour:

                  • Closing over the loop variable considered harmful
                  • Closing over the loop variable, part two

                  basically, it's the loop variable that is captured, not it's value. To get what you think you should get, do this:

                  foreach (var type in types)
                  {
                     var newType = type;
                     var sayHello = 
                              new PrintHelloType(greeting => SayGreetingToType(newType, greeting));
                     helloMethods.Add(sayHello);
                  }
                  

                  這篇關(guān)于在 lambda 表達(dá)式中使用 foreach 循環(huán)的迭代器變量 - 為什么會失敗?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

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

                    <tbody id='5mmAl'></tbody>
                    <bdo id='5mmAl'></bdo><ul id='5mmAl'></ul>

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

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

                          • <legend id='5mmAl'><style id='5mmAl'><dir id='5mmAl'><q id='5mmAl'></q></dir></style></legend>
                            主站蜘蛛池模板: 亚洲成人精品在线 | 国产视频一区二区在线观看 | 黄色网毛片 | 国产午夜精品一区二区三区在线观看 | 91av视频 | 91色视频在线观看 | 欧美久久久久久 | 日韩在线 | 久久久久国产一区二区三区四区 | 亚洲一区中文字幕 | 成人三级在线播放 | 欧美 日韩 国产 成人 在线 | 在线91| 最新中文字幕久久 | 欧美久久精品一级c片 | 成人午夜视频在线观看 | 久久久精品一区 | 亚洲欧美日韩精品久久亚洲区 | 久久久久一区 | 综合久久99| 午夜无码国产理论在线 | 热re99久久精品国产99热 | 亚洲精品v | 国产精品美女在线观看 | 国产一区二区三区在线看 | 伊人影院99 | 狠狠干天天干 | 韩日一区二区 | 成人午夜免费福利视频 | 99婷婷 | 国产激情一区二区三区 | 美女视频黄色片 | 天堂综合 | 亚洲综合电影 | 99久久婷婷国产综合精品首页 | www.亚洲一区 | 国产欧美一区二区三区国产幕精品 | 天堂一区二区三区 | 成人性视频免费网站 | 亚洲一页 | 亚洲国产视频一区二区 |