C# - 委託



C# 委託類似於 C 或 C++ 中的函式指標。委託是一種引用型別變數,它儲存對方法的引用。該引用可以在執行時更改。

委託特別用於實現事件和回撥方法。所有委託都隱式派生自System.Delegate類。

宣告委託

委託聲明確定可以由委託引用的方法。委託可以引用與委託簽名相同的方法。

例如,考慮一個委託:

public delegate int MyDelegate (string s);

前面的委託可用於引用任何具有單個字串引數並返回int型別變數的方法。

委託宣告的語法為:

delegate <return type> <delegate-name> <parameter list>

例項化委託

宣告委託型別後,必須使用new關鍵字建立委託物件並將其與特定方法關聯。建立委託時,傳遞給new表示式的引數的寫法類似於方法呼叫,但沒有方法的引數。例如:

public delegate void printString(string s);
...
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);

以下示例演示了委託的宣告、例項化和使用,該委託可用於引用採用整數引數並返回整數值的方法。

using System;

delegate int NumberChanger(int n);
namespace DelegateAppl {
   
   class TestDelegate {
      static int num = 10;
      
      public static int AddNum(int p) {
         num += p;
         return num;
      }
      public static int MultNum(int q) {
         num *= q;
         return num;
      }
      public static int getNum() {
         return num;
      }
      static void Main(string[] args) {
         //create delegate instances
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);
         
         //calling the methods using the delegate objects
         nc1(25);
         Console.WriteLine("Value of Num: {0}", getNum());
         nc2(5);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}

編譯並執行上述程式碼後,將產生以下結果:

Value of Num: 35
Value of Num: 175

委託的多播

可以使用“+”運算子組合委託物件。組合的委託會呼叫它組合的兩個委託。只能組合相同型別的委託。“-”運算子可用於從組合的委託中刪除元件委託。

使用委託的此屬性,您可以建立一個方法呼叫列表,當呼叫委託時將呼叫這些方法。這稱為委託的多播。以下程式演示了委託的多播:

using System;

delegate int NumberChanger(int n);
namespace DelegateAppl {
   class TestDelegate {
      static int num = 10;
      
      public static int AddNum(int p) {
         num += p;
         return num;
      }
      public static int MultNum(int q) {
         num *= q;
         return num;
      }
      public static int getNum() {
         return num;
      }
      static void Main(string[] args) {
         //create delegate instances
         NumberChanger nc;
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);
         
         nc = nc1;
         nc += nc2;
         
         //calling multicast
         nc(5);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}

編譯並執行上述程式碼後,將產生以下結果:

Value of Num: 75

使用委託

以下示例演示了委託的使用。委託printString可用於引用以字串作為輸入且不返回任何內容的方法。

我們使用此委託來呼叫兩個方法,第一個將字串列印到控制檯,第二個將字串列印到檔案:

using System;
using System.IO;

namespace DelegateAppl {

   class PrintString {
      static FileStream fs;
      static StreamWriter sw;
      
      // delegate declaration
      public delegate void printString(string s);

      // this method prints to the console
      public static void WriteToScreen(string str) {
         Console.WriteLine("The String is: {0}", str);
      }
      
      //this method prints to a file
      public static void WriteToFile(string s) {
         fs = new FileStream("c:\\message.txt",
         FileMode.Append, FileAccess.Write);
         sw = new StreamWriter(fs);
         sw.WriteLine(s);
         sw.Flush();
         sw.Close();
         fs.Close();
      }
      
      // this method takes the delegate as parameter and uses it to
      // call the methods as required
      public static void sendString(printString ps) {
         ps("Hello World");
      }
      
      static void Main(string[] args) {
         printString ps1 = new printString(WriteToScreen);
         printString ps2 = new printString(WriteToFile);
         sendString(ps1);
         sendString(ps2);
         Console.ReadKey();
      }
   }
}

編譯並執行上述程式碼後,將產生以下結果:

The String is: Hello World
廣告