教 程 目 录
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
使用代理
以下示例演示了delegate的用法.委托 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