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

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

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

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

      C# 事件处理程序委托中的闭包?

      Closures in C# event handler delegates?(C# 事件处理程序委托中的闭包?)
          <bdo id='XgPJS'></bdo><ul id='XgPJS'></ul>
          <tfoot id='XgPJS'></tfoot>

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

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

              • <legend id='XgPJS'><style id='XgPJS'><dir id='XgPJS'><q id='XgPJS'></q></dir></style></legend>
                  <tbody id='XgPJS'></tbody>
              • 本文介绍了C# 事件处理程序委托中的闭包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                目前我来自函数式编程背景,如果我不了解 C# 中的闭包,请见谅.

                I am coming from a functional-programming background at the moment, so forgive me if I do not understand closures in C#.

                我有以下代码来动态生成获取匿名事件处理程序的按钮:

                I have the following code to dynamically generate Buttons that get anonymous event handlers:

                for (int i = 0; i < 7; i++)
                {
                    Button newButton = new Button();
                
                    newButton.Text = "Click me!";
                
                    newButton.Click += delegate(Object sender, EventArgs e)
                    {
                        MessageBox.Show("I am button number " + i);
                    };
                
                    this.Controls.Add(newButton);
                }
                

                我希望文本 "I am button number" + i 在 for 循环的迭代中以 i 的值关闭.但是,当我实际运行程序时,每个 Button 都会显示 I am button number 7.我错过了什么?我用的是VS2005.

                I expected the text "I am button number " + i to be closed with the value of i at that iteration of the for loop. However, when I actually run the program, every Button says I am button number 7. What am I missing? I am using VS2005.

                所以我想我的下一个问题是,我如何捕获价值?

                So I guess my next question is, how do I capture the value?

                推荐答案

                要获得这种行为,你需要将变量复制到本地,而不是使用迭代器:

                To get this behavior, you need to copy the variable locally, not use the iterator:

                for (int i = 0; i < 7; i++)
                {
                    var inneri = i;
                    Button newButton = new Button();
                    newButton.Text = "Click me!";
                    newButton.Click += delegate(Object sender, EventArgs e)
                    {
                        MessageBox.Show("I am button number " + inneri);
                    };
                    this.Controls.Add(newButton);
                }
                

                在这个问题中更详细地讨论了推理.

                The reasoning is discussed in much greater detail in this question.

                这篇关于C# 事件处理程序委托中的闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <tbody id='RemEc'></tbody>

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

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

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

                        <tfoot id='RemEc'></tfoot>
                          <bdo id='RemEc'></bdo><ul id='RemEc'></ul>