从一些代码方法中,去学习C#委托

先来看看下面一个类中的一些方法:

 

 class Bc
    {
        public double Add(double number1, double number2)
        {
            return number1 + number2;
        }

        public double Multiply(double number1, double number2)
        {
            return number1 * number2;
        }

        public double Subtract(double number1, double number2)
        {
            return number1 - number2;
        }

        public double Division(double number1, double number2)
        {
            return number1 / number2;
        }
    }
Source Code

 

使用委托来应用:

 

class Bd
    {
        public delegate double CalcResult(double number1, double number2);
        public void TestDelegate()
        {
            Bc objBc = new Bc();

            CalcResult obj = new CalcResult(objBc.Multiply);
            Console.WriteLine(obj(2, 5));
        }
    }
Source Code

 

在控制台运行:

  

posted @ 2017-12-18 22:29  Insus.NET  阅读(815)  评论(4编辑  收藏  举报