- C# 基础教程
- C# - 主页
- C# - 概述
- C# - 环境
- C# - 程序结构
- C# - 基本语法
- C# - 数据类型
- C# - 类型转换
- C# - 变量
- C# - 常量
- C# - 运算符
- C# - 决策
- C# - 循环
- C# - 封装
- C# - 方法
- C# - 可空值
- C# - 数组
- C# - 字符串
- C# - 结构
- C# - 枚举
- C# - 类
- C# - 继承
- C# - 多态性
- C# - 运算符重载
- C# - 接口
- C# - 命名空间
- C# - 预处理器指令
- C# - 正则表达式
- C# - 异常处理
- C# - 文件 I/O
- C# 高级教程
- C# - 属性
- C# - 反射
- C# - 属性
- C# - 索引器
- C# - 委托
- C# - 事件
- C# - 集合
- C# - 泛型
- C# - 匿名方法
- C# - 不安全代码
- C# - 多线程
- C# 有用资源
- C# - 问题与解答
- C# - 快速指南
- C# - 有用的资源
- C# - 讨论
C# - 委托
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
使用委托
下面的例子演示了委托的使用。委托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
