C#委托是指向函数的指针,在C或C++。委托是一种引用类型的变量包含的方法。在运行时该引用是可以改变的。
委托尤其是用于实现事件的回调方法。所有委托都被隐式地从System.Delegate类派生。
声明委托
委托报关确定可以委托引用的方法。委托可以参考的方法,委托具有相同的签名。
例如,考虑一个委托:
public delegate int MyDelegate (string s);
前面的委托可用于引用任何方法,有一个字符串参数,并返回一个int型变量。
委托声明的语法是:
delegate <return type> <delegate-name> <parameter list>
实例委托
一旦被宣布为一个委托类型,委托对象必须使用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 by www.yiibai.com
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