C# 委托 Delegate
c# 委托 delegate
c# 中的委托(delegate)类似于 c 或 c++ 中函数的指针。委托(delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。
委托(delegate)特别用于实现事件和回调方法。所有的委托(delegate)都派生自 system.delegate 类。
1. 声明委托(delegate)
委托声明决定了可由该委托引用的方法。委托可指向一个与其具有相同标签的方法。
例如,假设有一个委托:
public delegate int mydelegate (string s);
上面的委托可被用于引用任何一个带有一个单一的 string 参数的方法,并返回一个 int 类型变量。
声明委托的语法如下:
delegate <return type> <delegate-name> <parameter list>
2. 范例化委托(delegate)
一旦声明了委托类型,委托对象必须使用 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)
{
// 创建委托范例
numberchanger nc1 = new numberchanger(addnum);
numberchanger nc2 = new numberchanger(multnum);
// 使用委托对象调用方法
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
3. 委托的多播(multicasting of a delegate)
委托对象可使用 "+" 运算符进行合并。一个合并委托调用它所合并的两个委托。只有相同类型的委托可被合并。"-" 运算符可用于从合并的委托中移除组件委托。
使用委托的这个有用的特点,您可以创建一个委托被调用时要调用的方法的调用列表。这被称为委托的 多播(multicasting),也叫组播。下面的程序演示了委托的多播:
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)
{
// 创建委托范例
numberchanger nc;
numberchanger nc1 = new numberchanger(addnum);
numberchanger nc2 = new numberchanger(multnum);
nc = nc1;
nc += nc2;
// 调用多播
nc(5);
console.writeline("value of num: {0}", getnum());
console.readkey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
value of num: 75
4. 委托(delegate)的用途
下面的范例演示了委托的用法。委托 printstring 可用于引用带有一个字符串作为输入的方法,并不返回任何东西。
我们使用这个委托来调用两个方法,第一个把字符串打印到控制台,第二个把字符串打印到文件:
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();
}
// 该方法把委托作为参数,并使用它调用方法
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


