e心e意

导航

C#委托

namespace DELEGATE
{
class Program
{
//声明委托
delegate double ProcessDelegate(double paragram1,double paragram2);
//函数1
static double Multiply(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 of D to divide:");
input = Console.ReadLine();
//判断输入的字符是否是大写的“M”
if (input == "M")
process = new ProcessDelegate(Multiply);
else
process = new ProcessDelegate(Divide );
Console.WriteLine("Result:{0}",process (param1 ,param2 ));
Console.ReadKey();
}
}
}

posted on 2014-11-26 10:59  e心e意  阅读(70)  评论(0)    收藏  举报