C#delegate委托
类似函数,却没有语句体。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { 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)); //输入M代表multiply , D代表divide 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(); } } }