委托(Daelegate)delegate是C#中的一種類型,它實(shí)際上是一個(gè)能夠持有對(duì)某個(gè)方法的引用的類。與其它的類不同,delegate類能夠擁有一個(gè)簽名(signature),并且它只能持有與它的簽名相匹配的方法的引用。它所實(shí)現(xiàn)的功能與C/C++中的函數(shù)指針十分相似。它允許你傳遞一個(gè)類A的方法m給另一個(gè)類B的對(duì)象,使得類B的對(duì)象能夠調(diào)用這個(gè)方法m。但與函數(shù)指針相比,delegate有許多函數(shù)指針不具備的優(yōu)點(diǎn)。首先,函數(shù)指針只能指向靜態(tài)函數(shù),而delegate既可以引用靜態(tài)函數(shù),又可以引用非靜態(tài)成員函數(shù)。在引用非靜態(tài)成員函數(shù)時(shí),delegate不但保存了對(duì)此函數(shù)入口指針的引用,而且還保存了調(diào)用此函數(shù)的類實(shí)例的引用。其次,與函數(shù)指針相比,delegate是面向?qū)ο蟆㈩愋桶踩⒖煽康氖芸兀╩anaged)對(duì)象。也就是說(shuō),runtime能夠保證delegate指向一個(gè)有效的方法,你無(wú)須擔(dān)心delegate會(huì)指向無(wú)效地址或者越界地址。
實(shí)現(xiàn)一個(gè)delegate是很簡(jiǎn)單的,通過(guò)以下3個(gè)步驟即可實(shí)現(xiàn)一個(gè)delegate:
step1.聲明一個(gè)delegate類型,它應(yīng)當(dāng)與你想要傳遞的方法具有相同的參數(shù)和返回值類型。
step2.創(chuàng)建delegate對(duì)象,并將你想要傳遞的函數(shù)作為參數(shù)傳入。
step3.在要實(shí)現(xiàn)異步調(diào)用的地方,通過(guò)上一步創(chuàng)建的對(duì)象來(lái)調(diào)用方法。
下面是一個(gè)簡(jiǎn)單的例子:
using System;

namespace MyApp


{

/**////
/// MyDelegateTest 的摘要說(shuō)明。
///
public class MyDelegateTest

{
// 步驟1,聲明delegate類型
public delegate void MyDelegate(string name);

// 這是我們欲傳遞的方法,它與MyDelegate具有相同的參數(shù)和返回值類型
public static void MyDelegateFunc(string name)

{
Console.WriteLine("靜態(tài)方法: Hello, {0}", name);
}

public static void Main()

{
// 步驟2,創(chuàng)建delegate對(duì)象
MyDelegate md = new MyDelegate(MyDelegateTest.MyDelegateFunc);

// 步驟3,調(diào)用delegate
md("王銘");

//如何調(diào)用類成員方法
myclass mc=new myclass();
md=new MyDelegate(mc.mymethod);
md("Wang Ming");

//委托可以作為方法的參數(shù)
WL(md);

Console.ReadLine(); //為了顯示控制臺(tái)
}

public static void WL(MyDelegate mywl)

{
mywl("Ming Wang");
}


public MyDelegateTest()

{}
}

public class myclass

{
public void mymethod(string name)

{
Console.WriteLine("類成員方法: Hello,{0}",name);
}
}
}

下面是一個(gè)比較復(fù)雜的例子,使用委托技術(shù),BubbleSorter.Sort方法可以排序任何數(shù)組,前提是數(shù)組成員類提供比較的方法,本例中Employee提供RhsIsGreater方法。
using System;

namespace Wrox.ProfCSharp.AdvancedCSharp


{
//步驟1 定義Delegated類型
delegate bool CompareOp(object lhs, object rhs);

class MainEntryPoint

{
static void Main()

{
Employee [] employees =

{
new Employee("Karli Watson", 20000),
new Employee("Bill Gates", 10000),
new Employee("Simon Robinson", 25000),
new Employee("Mortimer", (decimal)1000000.38),
new Employee("Arabel Jones", 23000),
new Employee("Avon from 'Blake's 7'", 50000)};
//步驟2 創(chuàng)建Delegate對(duì)象,傳入要調(diào)用的函數(shù)作為參數(shù)
CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
BubbleSorter.Sort(employees, employeeCompareOp);

for (int i=0 ; i<employees.Length ; i++)
Console.WriteLine(employees[i].ToString());
Console.ReadLine();
}
}

class Employee // : object

{
private string name;
private decimal salary;

public Employee(string name, decimal salary)

{
this.name = name;
this.salary = salary;
}

public override string ToString()

{
return string.Format(name + ", {0:C}", salary);
}

public static bool RhsIsGreater(object lhs, object rhs)

{
Employee empLhs = (Employee) lhs;
Employee empRhs = (Employee) rhs;
return (empRhs.salary > empLhs.salary) ? true : false;
}
}

class BubbleSorter

{
static public void Sort(object [] sortArray, CompareOp gtMethod)

{
for (int i=0 ; i<sortArray.Length ; i++)

{
for (int j=i+1 ; j<sortArray.Length ; j++)

{
if (gtMethod(sortArray[j], sortArray[i]))//調(diào)用Delegate

{
object temp = sortArray[i];
sortArray[i] = sortArray[j];
sortArray[j] = temp;
}
}
}
}
}
}