C#中的委托实现
using System;
namespace MSPress.DelegateSample
{
public delegate void PrintCallback(int number); //定义一个名为PrintCallback的委托,返回void类型
public class Printer
{
private PrintCallback _print;
public PrintCallback PrintCallback
{
get
{
return _print;
}
set
{
_print = value;
}
}
}
public class Driver
{
private void PrintInteger(int number)
{
Console.WriteLine("From PrintInteger:The number is {0}.",number);
}
static void Main(string [] args)
{
Driver driver = new Driver();
Printer printer = new Printer();
printer.PrintCallback = new PrintCallback(driver.PrintInteger);//将委托绑定到PrintInteger方法
printer.PrintCallback(10);
printer.PrintCallback(100);
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
}
}