using System; using System.Collections.Generic; using System.Text;namespace DelegateDemo {class Class1{delegate double processDelegate(double db1, double db2);static double Multiply(double db1, double db2){return db1 * db2;}static double Divide(double db1, double db2){return db1 / db2;}static void NamedMethod(string strInput,double dbNum1,double dbNum2){processDelegate process; if (strInput == "M")process = new processDelegate(Multiply);elseprocess = new processDelegate(Divide);Console.WriteLine("结果为:{0}", process(dbNum1, dbNum2));}static void AnonymousMethod(string strInput, double dbNum1, double dbNum2){processDelegate process;if (strInput == "M")process = delegate(double db1, double db2){return db1 * db2;};elseprocess = delegate(double db1, double db2) { return db1 / db2; };Console.WriteLine("结果为:{0}", process(dbNum1, dbNum2));}/// <summary>/// 应用程序的主入口点。/// </summary> [STAThread]static void Main(string[] args){//// TODO: 在此处添加代码以启动应用程序Console.WriteLine("请输入两个小数,用逗号分割");string strInput = Console.ReadLine();int commmaPos = strInput.IndexOf(',');double dbNum1 = Convert.ToDouble(strInput.Substring(0, commmaPos));double dbNum2 = Convert.ToDouble(strInput.Substring(commmaPos + 1));Console.WriteLine("输入M表示乘法,或者D表示除法");strInput = (Console.ReadLine()).ToUpper();//使用命名方法Console.WriteLine("使用命名方法委托");NamedMethod(strInput,dbNum1,dbNum2); //使用匿名方法Console.WriteLine("使用匿名方法委托");AnonymousMethod(strInput, dbNum1, dbNum2); }}}