使用委托调用函数。

using System;
namespace a
{
	class Program
	{
		delegate double ProcessDelegate(double param1,double param2);
		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 or D to divide:");
			input =Console.ReadLine();
			if(input=="M")
				process=new ProcessDelegate(Multiply);
			else
				process=new ProcessDelegate(Divide);
			Console.WriteLine("Result:{0}",process(param1,param2));
			Console.ReadKey();
		}
	}
}

相关文章:

  • 2022-03-04
  • 2022-12-23
  • 2021-06-10
  • 2021-07-29
  • 2021-08-14
  • 2021-12-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2022-02-25
  • 2022-01-03
  • 2022-01-05
相关资源
相似解决方案