现在的位置: 首页 > 综合 > 正文

C#1.0/2.0/3.0中的委托

2013年03月06日 ⁄ 综合 ⁄ 共 3467字 ⁄ 字号 评论关闭

委托和事件是.net程序设计中一个难度中等、重要性高等的知识点,掌握好委托和事件,是一个中级程序员的标识之一。

不说废话,直接上代码,要看效果请建立一个控制台程序,然后把下面代码拷贝并替换Program类。

/* *************************************
 * 公司:浙江新能量科技有限公司 
 * 网站:
http://www.freshpower.com.cn
 * 电力商务网:
http://www.freshpower.cn
 * ------------------------------------
 * 作者:李中华 lizhh@freshpower.cn
 * 日期:2008-7-2 
 * ************************************
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CEDU
{
    
delegate void TestDelegate1();
    
delegate void TestDelegate2(string msg);
    
delegate void TestDelegate3(string msg, int i);
    
class Program
    
{
        
//C#1.0:定义事件
        static event TestDelegate1 td1;
        
//C#2.0:定义事件
        static event TestDelegate1 td2;
        
//C#3.0:定义事件
        static event TestDelegate1 td3;

        
static void Main(string[] args)
        
{
            InitEvent();
            TM1();
            TM2(
"带参数的测试");
            TM3(
"带参数的测试"0);
            Console.ReadKey();
        }

        
与事件有关的成员

        
委托1有关的成员

        
委托2有关的成员

        
委托3有关的成员
    }

}

抱歉!评论已关闭.