欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-14 13:26:10
复制代码 代码如下:delegate double processdelegate(double param1, double param2);   ...

复制代码 代码如下:

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();
        }