Value is 0.5 result of DoubleOpration is 3;
Value is 3.2 result of DoubleOpration is 10.24;
在上例中,我們如果采用匿名方法,代碼就會如下:
using System;
namespace gosoa.com.cn
{
public class delegateTest
{
delegate double DoubleOpration(double x);
static void printNumber(DoubleOpration dp,double x)
{
double result=dp(x);
Console.WriteLine(
"value is {0}, result of DoubleOpration is {1}:",x,result
);
}
static void Main()
{
DoubleOpration doptionOne =delegate(double x){return x*2;};
DoubleOpration doptionTwo =delegate(double x){return x*x;};
printNumber(doptionOne,1.5);
printNumber(doptionTwo,3.2);
}
}
}
委托,還有一種情況,是多播委托。這個在以後我們應用到的時候,會學習到。