委托概述:
与C/C++中的函数指针类似,把方法用对象封装起来,以便在运行时间接的绑定该方法。委托的使用:
关键字: delegatepublic delegate bool ComparsonHandler(int x, int y);
using System;
using System.Collections.Generic;
using System.Text;
namespace Delegate
{
class DelegateSample
{
public delegate bool PrintHeaderHandler(int x, int y);
public static bool PrintHeaderA(int x, int y)
{
Console.WriteLine("A func ===> X===={0}, Y===={1}", x, y);
return true;
}
public static bool PrintHeaderB(int x, int y)
{
Console.WriteLine("B func ===> X::::{0}, Y::::{1}", x, y);
return true;
}
public static void Tester(int a, int b, PrintHeaderHandler printer)
{
printer(a, b);
}
}
class Program
{
static void Main(string[] args)
{
DelegateSample.Tester(1, 2, DelegateSample.PrintHeaderA);
DelegateSample.Tester(5, 10, DelegateSample.PrintHeaderB);
Console.ReadKey(