为什么委托变量中赋值时只能给static函数
class Program
{
delegate double ProcessDelegate(double param1, double param2);//委托的声明和函数的声明是相同的,不同的是前面要加委托的关键字DELEGATE
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;
process = null;
Console.WriteLine("Enter 2 numebles separated with a comma:");
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');
Console.WriteLine("The position is {0}",commaPos );
double param1 = Convert.ToDouble(input.Substring(0, commaPos)) ;
double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1)) ;
Console.WriteLine("param1 :{0} \n param2 :{1}",param1 ,param2 );
Console.WriteLine("Enter M to Multiply or D to Divide");
input = Console.ReadLine();
if (input == "M")
{
process = new ProcessDelegate(Multiply );
}
else //注意这里ELSE是不能去掉的,因为如果input='a'的话,那么process就是一个为负值了。
if(input =="D")
{
process = new ProcessDelegate(Divide);
}
Console.WriteLine("result is {0}", process(param1, param2)); Console.ReadKey();