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) { //创建委托实例 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
委托的多播
可以使用"+"运算符组合委托对象。组合后的委托会调用由其组成的两个委托。只有相同类型的委托才能组合。"-"运算符可用于从组合后的委托中移除组件委托。
使用委托的此属性,您可以创建一个方法调用列表,这些方法将在调用委托时被调用。这称为委托的多播。以下程序演示了委托的多播 -
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
使用委托
以下示例演示了委托的使用。委托 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