码迷,mamicode.com
首页 > Windows程序 > 详细

C#的委托

时间:2015-08-28 02:04:28      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace codeTest
{
    delegate void NumDelegate(int num);
    class Program
    {

        static void Main(string[] args)
        {
            AClass _a = new AClass();
            _a.cwdelegate = _a.AddNum;
            _a.cwdelegate(20);
            NumDelegate num20delegate = new NumDelegate(Add20);
            Console.WriteLine("-------------------------------------------");
            NumDelegate adddelegate =new NumDelegate(_a.AddNum);
            NumDelegate subdelegate = new NumDelegate(_a.SubNum);
            NumDelegate num30delegate = adddelegate + subdelegate;
            num30delegate(30);
            Console.WriteLine("-------------------------------------------");
            num30delegate += num20delegate;
            num30delegate(30);
            Console.WriteLine("-------------------------------------------");
            num30delegate += adddelegate;
            //去掉最后一个adddelegate
            num30delegate -= adddelegate;
            num30delegate(30);
            Console.ReadLine();
        }
        static void Add20(int num)
        {
            Console.WriteLine(string.Format("Add20: {0}", num));
        }
    }

    class AClass
    {
        public int result = 100;
        public delegate void CWDelegate(int num);
        public CWDelegate cwdelegate;
        public void AddNum(int num)
        {
            Console.WriteLine(string.Format("AddNum: {0}",num));
        }

        public void SubNum(int num)
        {
            Console.WriteLine(string.Format("SubNum: {0}", num));
        }
    }

}

 

C#的委托

标签:

原文地址:http://www.cnblogs.com/lgxlsm/p/4765248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!