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();
}
上一篇:页面间隔半秒钟更新时间 Asp.net使用Comet开发http长连接示例分享
栏 目:.NET代码
下一篇:ASP.NET Core 3.x 并发限制的实现代码
本文标题:C#委托初级使用的实例代码
本文地址:http://www.codeinn.net/misctech/82751.html