C# 中的委托類似于 C 或 C++ 中指向函數(shù)的指針。委托表示引用某個方法的引用類型變量,運行時可以更改引用對象。
特別地,委托可以用于處理事件或回調(diào)函數(shù)。并且,所有的委托類都是從 System.Delegate 類繼承而來。
聲明委托時,需要定義能夠被委托所引用的方法,任意委托可以引用與該委托擁有相同簽名的方法。如:
public delegate int MyDelegate (string s);
上述委托可以用于引用任何一個以字符型為參數(shù)的方法,且返回值類型為整型。
聲明委托的句法規(guī)則為:
delegate <return type> <delegate-name> <parameter list>
聲明委托之后,必須使用 new 關(guān)鍵字和一個特定的方法來創(chuàng)建一個委托對象。創(chuàng)建時,傳遞到 new 語句的參數(shù)寫法與方法調(diào)用相同,但是不帶有參數(shù),例如:
public delegate void printString(string s);
...
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);
下述示例演示了委托的聲明、實例化,此處的委托用于引用一個帶有一個整型參數(shù)的方法,且該方法返回一個整型值。
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)
{
// 創(chuàng)建委托實例
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
// 使用委托對象調(diào)用方法
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
編譯執(zhí)行上述代碼,得到如下結(jié)果:
Value of Num: 35
Value of Num: 175
委托對象可通過 "+" 運算符進行合并。一個合并委托可以調(diào)用它所合并的兩個委托,但只有相同類型的委托可被合并。"-" 運算符則可用于從合并的委托中移除其中一個委托。
利用委托的這種特性,可以創(chuàng)建一個委托被調(diào)用時所涉及的方法的調(diào)用列表。這被稱為委托的多播,也叫組播。下面的程序演示了委托的多播:
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)
{
// 創(chuàng)建委托實例
NumberChanger nc;
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
nc = nc1;
nc += nc2;
// 調(diào)用多播
nc(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
編譯執(zhí)行上述代碼,得到如下結(jié)果:
Value of Num: 75
下面的示例演示了委托的作用,示例中的 printString 委托可用于引用帶有一個字符串作為輸入的方法,且不返回數(shù)據(jù)。
我們使用這個委托來調(diào)用兩個方法,第一個方法將字符串輸出到控制臺,第二個方法將字符串輸出到文件:
using System;
using System.IO;
namespace DelegateAppl
{
class PrintString
{
static FileStream fs;
static StreamWriter sw;
// 委托聲明
public delegate void printString(string s);
// 該方法打印到控制臺
public static void WriteToScreen(string str)
{
Console.WriteLine("The String is: {0}", str);
}
// 該方法打印到文件
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();
}
// 該方法把委托作為參數(shù),并使用它調(diào)用方法
// 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();
}
}
}
編譯執(zhí)行上述代碼,得到如下結(jié)果:
The String is: Hello World
更多建議: