using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Convert;
namespace firstCsharp
{
class Program
{
delegate double processdelegate(double param1, double param2);
static double Multiply(double param1, double param2) => param1 * param2;
static double Divide(double param1, double parma2) => param1 / parma2;
static void Main(string[] args)
{
processdelegate process=null;
WriteLine("please enter two number ,seperator by ',' :");
string input = ReadLine();
int compos = input.IndexOf(',');
double param1 = ToDouble(input.Substring(0, compos));
double param2 = ToDouble(input.Substring(compos + 1));
WriteLine("Enter M for Multiply,D for Divide.");
input = ReadLine().ToUpper();
if(input=="M")
{
process = new processdelegate(Multiply);
}
else if(input=="D")
{
process = new processdelegate(Divide);
}
WriteLine($"result: {process(param1, param2)}");
ReadKey();
}
}
}
processdelegata委托的声明要指定返回类型和参数类型。声明processdelegata变量process时要初始化,这里设为null。
调用相应的函数使用process=new processdelegata(函数名),然后直接调用process。