目录

C# - 代表(Delegates)

C#委托类似于C或C ++中的函数指针。 delegate是一个引用类型变量,用于保存对方法的引用。 可以在运行时更改引用。

代表特别用于实现事件和回调方法。 所有委托都是从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

代表的多播

可以使用“+”运算符组合委托对象。 组合委托调用它组成的两个委托。 只能组成相同类型的代理。 “ - ”运算符可用于从组合委托中删除组件委托。

使用委托的此属性,您可以创建在调用委托时将调用的方法的调用列表。 这称为代表的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) {
         //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# - Events.下一篇>
↑回到顶部↑
WIKI教程 @2018