c# delegate
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication8
{
//定义委托
public delegate bool CompareDelegate(int a, int b);
public class MyCompare
{
/// <summary>
/// 定义一个方法,符合上面定义的委托所要求的方法细节,返回值为bool型,两个参数分别为int型
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static bool CompareMethod(int x, int y)
{
bool result = x > y ? true : false;
return result;
}
}
class Program
{
static void Main(string[] args)
{
//实例化委托并初始化。注意初始化时候,必须包含一个方法的名称,而且这个方法必须符合最初定义委托时候的签名(参数类型和返回值)
CompareDelegate myDelegate = new CompareDelegate(MyCompare.CompareMethod);
//现在我们使用委托实例来比较两个数a和b的的大小
int a = 10;
int b = 15;
bool isBigger = myDelegate(a, b);
Console.WriteLine(" a > b ?: " + isBigger);
Console.Read();
}
}
}