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

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

  • <legend id='oVtRM'><style id='oVtRM'><dir id='oVtRM'><q id='oVtRM'></q></dir></style></legend>
  • <tfoot id='oVtRM'></tfoot>

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

        C# 中的代表

        Delegates in C#(C# 中的代表)

            1. <legend id='IgYEx'><style id='IgYEx'><dir id='IgYEx'><q id='IgYEx'></q></dir></style></legend>
                <tbody id='IgYEx'></tbody>

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

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

                  <tfoot id='IgYEx'></tfoot>
                  本文介绍了C# 中的代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在理解 C# 中的委托如何工作时遇到了一些麻烦.我有很多代码示例,但我仍然无法正确掌握.

                  I`m having some trouble in understanding how delegates in C# work. I have many code examples, but i still could not grasp it properly.

                  有人可以用简单的英语"向我解释吗?当然!代码示例会有所帮助,但我认为我需要更多地描述它的工作方式/原因.

                  Can someone explain it to me in "plain english"? Of course! examples of code will help, but i think i need more of a description of how/why it works.

                  好吧,问题是:为什么代表工作?什么是整个流程的流程图"?

                  Well, the question is: why does delegates work? What is a "flow chart" of the entire process?

                  使用委托的先决条件是什么?

                  What are the pre-requisites of using delegates?

                  我希望这能让问题更清楚.

                  I hope this makes the question clearer.

                  推荐答案

                  考虑委托的一种方式类似于对函数的引用.例如,假设您在窗口中有一个按钮,并且您希望在单击该按钮时发生一些事情.您可以为按钮的 Click 事件附加一个委托,并且每当用户单击此按钮时,您的函数就会被执行.

                  One way to think about a delegate is like a reference to a function. For example, say you have a button in a window, and you want something to happen when the button is clicked. You can attach a delegate to the Click event of the button, and whenever the user clicks this button, your function will be executed.

                  class MyWindow : Window
                  {
                      Button _button;
                  
                      public MyWindow()
                      {
                          _button = new Button();
                          // place the button in the window
                          _button.Click += MyWindow.ButtonClicked;
                      }
                  
                      static void ButtonClicked(object sender, RoutedEventArgs e)
                      {
                          MessageBox.Show("Button Clicked");
                      }
                  }
                  

                  注意我是如何使 ButtonClicked 成为静态函数的——接下来我想说明一下非静态函数.假设 ButtonClicked 是一个非静态成员:

                  Notice how I make ButtonClicked a static function - I want to make a point about non-static functions next. Suppose instead that ButtonClicked is a non-static member:

                  class MyWindow : Window
                  {
                      Button _button;
                      int _numClicked = 0;
                  
                      public MyWindow()
                      {
                          this._button = new Button();
                          // place the button in the window
                          this._button.Click += this.ButtonClicked;
                      }
                  
                      void ButtonClicked(object sender, RoutedEventArgs e)
                      {
                          this._numClicked += 1;
                          MessageBox.Show("Button Clicked " + this._numClicked + " times");
                      }
                  }
                  

                  现在委托包含对函数ButtonClicked"的引用和调用该方法的实例this".MyWindow 构造函数中的实例this"和 ButtonClicked 中的this"是一样的.

                  Now the delegate contains both a reference to the function "ButtonClicked" and the instance, "this", which the method is called on. The instance "this" in the MyWindow constructor and "this" in ButtonClicked are the same.

                  这是一个称为 closures 概念的特定案例,它允许在创建委托时保存"状态 - 当前对象、局部变量等.在上面的示例中,我们在委托的构造函数中使用了this".我们可以做的不止这些:

                  This is a specific case of a concept known as closures which allows "saving" the state - the current object, local variables, etc. - when creating a delegate. In the above example, we used "this" from the constructor in the delegate. We can do more than that:

                  class MyWindow : Window
                  {
                      Button _button;
                      int _numClicked = 0;
                  
                      public MyWindow(string localStringParam)
                      {
                          string localStringVar = "a local variable";
                          this._button = new Button();
                          // place the button in the window
                          this._button.Click += new RoutedEventHandler(
                              delegate(object sender, RoutedEventArgs args)
                              {
                                  this._numClicked += 1;
                                  MessageBox.Show("Param was: " + localStringParam + 
                                       " and local var " + localStringVar +
                                       " button clicked " + this._numClicked + " times");
                              });
                      }
                  }
                  

                  这里我们创建了一个匿名委托 - 一个没有给出明确名称的函数.引用此函数的唯一方法是使用 RoutedEventHandler 委托对象.此外,该函数存在于 MyWindow 构造函数的范围内,因此它可以访问所有本地参数、变量和成员实例this".即使在 MyWindow 构造函数退出后,它也会继续持有对局部变量和参数的引用.

                  Here we created an anonymous delegate - a function which is not given an explicit name. The only way to refer to this function is using the RoutedEventHandler delegate object. Furthermore, this function exists in the scope of the MyWindow constructor, so it can access all local parameters, variables, and the member instance "this". It will continue to hold references to the local variables and parameters even after the MyWindow constructor exits.

                  作为旁注,委托还将持有对对象实例的引用 - this" - 即使在对类 a 的所有其他引用都已删除之后.因此,为了确保一个类被垃圾回收,所有非静态成员方法的委托(或在一个范围内创建的委托)都应该被删除.

                  As a side note, the delegate will also hold a reference to the object instance - "this" - even after all other references to the class a removed. Therefore, to ensure that a class is garbage collected, all delegates to a non-static member method (or delegates created in the scope of one) should be removed.

                  这篇关于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() 和泛型:错误绑定到目标方法)

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

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

                        • <small id='UfJFB'></small><noframes id='UfJFB'>