namespace ConsoleApp1
{
class Program
{
delegate int myDelegate(int t); //定义委托 返回值类型 入参类型
static int num = 10;
static void Main(string[] args)
{
myDelegate myDelegate1 = new myDelegate(Test1); //实例化委托
myDelegate myDelegate2 = new myDelegate(Test2); //实例化委托
int theOne = myDelegate1(10); //调用委托,传入参数10
int theTwo = myDelegate2(3); //调用委托,传入参数3
Console.WriteLine("theOne:"+theOne);
Console.WriteLine("theTwo:" + theTwo);
Console.ReadLine();
}
static int Test1(int p)
{
num += p;
return num;
}
static int Test2(int p)
{
num *= p;
return num;
}
}
}
运行控制台程序,打印结果如下:
theOne:20
theTwo:60