<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 帶有命名方法的委托與帶有匿名方法的委托(C# 編程指南) [委托](https://msdn.microsoft.com/zh-cn/library/900fyy8e.aspx)可以與命名方法關聯。使用命名方法對委托進行實例化時,該方法將作為參數傳遞,例如: ``` // Declare a delegate: delegate void Del(int x); // Define a named method: void DoWork(int k) { /* ... */ } // Instantiate the delegate using the method as a parameter: Del d = obj.DoWork; ``` 這被稱為使用命名的方法。使用命名方法構造的委托可以封裝[靜態](https://msdn.microsoft.com/zh-cn/library/98f28cdx.aspx)方法或實例方法。在早期版本的 C# 中,命名方法是對委托進行實例化的唯一方式。但是,在不希望付出創建新方法的系統開銷時,C# 使您可以對委托進行實例化,并立即指定委托在被調用時將處理的代碼塊。代碼塊可以包含 lambda 表達式或匿名方法。有關更多信息,請參見 [匿名函數(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb882516.aspx)。 ## 備注 作為委托參數傳遞的方法必須與委托聲明具有相同的簽名。 委托實例可以封裝靜態或實例方法。 盡管委托可以使用 [out](https://msdn.microsoft.com/zh-cn/library/t3c3bfhx.aspx) 參數,但建議您不要將其用于多路廣播事件委托,因為您無法知道哪個委托將被調用。 ## 示例 1 以下是聲明及使用委托的一個簡單示例。注意,委托 Del 和關聯的方法 MultiplyNumbers 具有相同的簽名 ``` // Declare a delegate delegate void Del(int i, double j); class MathClass { static void Main() { MathClass m = new MathClass(); // Delegate instantiation using "MultiplyNumbers" Del d = m.MultiplyNumbers; // Invoke the delegate object. System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':"); for (int i = 1; i <= 5; i++) { d(i, 2); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } // Declare the associated method. void MultiplyNumbers(int m, double n) { System.Console.Write(m * n + " "); } } /* Output: Invoking the delegate using 'MultiplyNumbers': 2 4 6 8 10 */ ``` ## 示例 2 在下面的示例中,一個委托被同時映射到靜態方法和實例方法,并分別返回特定的信息。 ``` // Declare a delegate delegate void Del(); class SampleClass { public void InstanceMethod() { System.Console.WriteLine("A message from the instance method."); } static public void StaticMethod() { System.Console.WriteLine("A message from the static method."); } } class TestSampleClass { static void Main() { SampleClass sc = new SampleClass(); // Map the delegate to the instance method: Del d = sc.InstanceMethod; d(); // Map to the static method: d = SampleClass.StaticMethod; d(); } } /* Output: A message from the instance method. A message from the static method. */ ``` ## 請參閱 [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [委托(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/ms173171.aspx) [匿名方法(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/0yw3tz5k.aspx) [如何:合并委托(多路廣播委托)(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/ms173175.aspx) [事件(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/awbftdfh.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看