亲宝软件园·资讯

展开

委托初级使用 C#委托初级使用的实例代码

人气:0
想了解C#委托初级使用的实例代码的相关内容吗,在本文为您仔细讲解委托初级使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,委托,实例,下面大家一起来学习吧。

复制代码 代码如下:

delegate double ProcessDelegate(double param1, double param2);

        static double Muliply(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.Equals("M"))
            {
                process = new ProcessDelegate(Muliply);
            }
            else {
                process = new ProcessDelegate(Divide);
            }
            Console.WriteLine("Result: {0}",process(param1,param2));
            Console.ReadKey();
        }

加载全部内容

相关教程
猜你喜欢
用户评论