C# - 代表
C# 委托类似于 C 或 C++ 中的函数指针。一个代表 是一个引用类型变量,它保存对方法的引用。可以在运行时更改引用。
委托特别用于实现事件和回调方法。所有委托都隐式派生自 System.Delegate 类。
声明委托
委托声明决定了委托可以引用的方法。委托可以引用与委托具有相同签名的方法。
例如,考虑一个委托 -
public delegate int MyDelegate (string s);
前面的委托可用于引用具有单个 string 的任何方法 参数并返回一个 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
C语言