C#委托初级使用的实例代码
delegate double processdelegate(double param1, double param2);
static double muliply(double param1, double param2)
{
return param1 * param2;
}
static double divide(double param1, double param2)
{
return param1 / param2;
}
static void main(string[] args)
{
processdelegate process;
console.writeline("enter 2 numbers separated with a comma:");
string input = console.readline();
int commapos = input.indexof(",");
double param1 = convert.todouble(input.substring(0,commapos));
double param2 = convert.todouble(input.substring(commapos+1,input.length -commapos -1));
console.writeline("enter m to multiply or d to divide:");
input = console.readline();
if (input.equals("m"))
{
process = new processdelegate(muliply);
}
else {
process = new processdelegate(divide);
}
console.writeline("result: {0}",process(param1,param2));
console.readkey();
}