Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12300645
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: 系统运维

2011-09-28 17:17:30

namespace @delegate
{   
    public class MathClass
    {
        public static long Add(int i, int j)       // static
        {
            return (i + j);
        }

        public static long Multiply(int i, int j)  // static
        {
            return (i * j);
        }
    }

    class Program
    {

        delegate long Del(int i, int j);  // declare the delegate type

        static void Main(string[] args)
        {
            Del operation;  // declare the delegate variable

            operation = MathClass.Add;       // set the delegate to refer to the Add method
            long sum = operation(11, 22);             // use the delegate to call the Add method

            operation = MathClass.Multiply;  // change the delegate to refer to the Multiply method
            long product = operation(30, 40);         // use the delegate to call the Multiply method

            System.Console.WriteLine("11 + 22 = " + sum);
            System.Console.WriteLine("30 * 40 = " + product);
            Console.ReadLine(); //它使程序在按 Enter 键之前暂停
        }
    }
}

 

 

委托是一个函数模板,可以将与它原型相同的函数指针赋给一个委托,然后通过委托加参数来实现该函数功能。

       image

阅读(3108) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~