一个C#实现的最简单的委托例子
using System;
namespace DelegateSample
{
public delegate void PrintCallback(int number);
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);
//使用属性触发委托事件
printer.PrintCallback(10);
printer.PrintCallback(100);
Console.WriteLine("press Enter to exit");
Console.ReadLine();
}
}
}
结果:
From PrintInteger:The number is 10.
From PrintInteger:The number is 100.
press Enter to exit
namespace DelegateSample
{
public delegate void PrintCallback(int number);
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);
//使用属性触发委托事件
printer.PrintCallback(10);
printer.PrintCallback(100);
Console.WriteLine("press Enter to exit");
Console.ReadLine();
}
}
}
结果:
From PrintInteger:The number is 10.
From PrintInteger:The number is 100.
press Enter to exit
我来自:向东博客