全部博文(1293)
分类: 系统运维
2011-09-28 17:17:30
namespace @delegate
{
public class MathClass
{
public static long Add(int i, int j) // static
{
return (i + j);
}
public static long Multiply(int i, int j) // static
{
return (i * j);
}
}
class Program
{
delegate long Del(int i, int j); // declare the delegate type
static void Main(string[] args)
{
Del operation; // declare the delegate variable
operation = MathClass.Add; // set the delegate to refer to the Add method
long sum = operation(11, 22); // use the delegate to call the Add method
operation = MathClass.Multiply; // change the delegate to refer to the Multiply method
long product = operation(30, 40); // use the delegate to call the Multiply method
System.Console.WriteLine("11 + 22 = " + sum);
System.Console.WriteLine("30 * 40 = " + product);
Console.ReadLine(); //它使程序在按 Enter 键之前暂停
}
}
}
委托是一个函数模板,可以将与它原型相同的函数指针赋给一个委托,然后通过委托加参数来实现该函数功能。