免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1775 | 回复: 0
打印 上一主题 下一主题

delegate MSDN [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-23 03:51 |只看该作者 |倒序浏览

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

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP