C#体验·委托和事件

博客介绍了用 csc.exe 生成代码程序,重点讲解了 C# 中委托的使用。需在包含事件声明的类中声明委托型对象,使用时要保持声明、实例化、调用三个步骤,并通过 EventClass 类举例说明。最后提到 Java 中没有委托。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要为类构造一个事件,必须用 event 来声明一个 delegate 型的字段,如:

puclic calss Test{
         public delegate EventHandler(object sender, EventArgs e); //声明为delegate 型的事件;
}

然后要指定一个事件的名称,并写出处理语句:
        public event  EventHandler Load

在创建类的实例后定义这个 “Load”事件:
        Test m=new Test();
        m.load=new EventHandler(m_Load);
        void m_Load(object sender, EventArgs e)
        {
                MessageBox.Show(" this is a class event");
        }      


再看看下面的完整的一段代码:

 1None.gifusing System;
 2None.gif
 3None.gifclass TestClass
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    static void Main(string[] args)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
 7InBlock.gif        EventClass myEventClass = new EventClass();
 8InBlock.gif        myEventClass.CustomEvent += new EventClass.CustomEventHandler(CustomEvent1); // 关联事件句柄;
 9InBlock.gif        myEventClass.CustomEvent += new EventClass.CustomEventHandler(CustomEvent2); 
10InBlock.gif        myEventClass.InvokeEvent();
11InBlock.gif        myEventClass.CustomEvent -= new EventClass.CustomEventHandler(CustomEvent2);
12InBlock.gif        myEventClass.InvokeEvent();
13InBlock.gif        myEventClass.Load += new EventClass.CustomEventHandler(Load1);
14InBlock.gif        myEventClass.onLoad();
15ExpandedSubBlockEnd.gif    }

16InBlock.gif
17InBlock.gif    private static void CustomEvent1(object sender, EventArgs e)
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        Console.WriteLine("Fire Event 1");
20ExpandedSubBlockEnd.gif    }

21InBlock.gif
22InBlock.gif    private static void CustomEvent2(object sender, EventArgs e)
23ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
24InBlock.gif        Console.WriteLine("Fire Event 2");
25ExpandedSubBlockEnd.gif    }

26InBlock.gif    private static void Load1(object sender, EventArgs e)
27ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
28InBlock.gif        Console.WriteLine("Current background color is {0}. Please input:", System.Console.BackgroundColor.ToString());
29ExpandedSubBlockEnd.gif    }

30ExpandedBlockEnd.gif}

31None.gif
32None.gifpublic class EventClass
33ExpandedBlockStart.gifContractedBlock.gifdot.gif{
34InBlock.gif    public delegate void CustomEventHandler(object sender, EventArgs e);//首先定义一个委托类型的对象CustomEventHandler
35InBlock.gif
36InBlock.gif    //用delegate数据类型声明事件,要用event关键字,这里定义了两个字件;
37InBlock.gif    public event CustomEventHandler CustomEvent;
38InBlock.gif    public event CustomEventHandler Load;
39InBlock.gif        
40InBlock.gif    public void InvokeEvent()
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        CustomEvent(this, EventArgs.Empty);
43ExpandedSubBlockEnd.gif    }

44InBlock.gif    public void onLoad()
45ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
46InBlock.gif        Console.BackgroundColor = ConsoleColor.Red;
47InBlock.gif        Load(this, EventArgs.Empty);
48InBlock.gif        string s = Console.ReadLine();
49InBlock.gif        if (s != "yuping")
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            Console.WriteLine("You must type 'yuping' for change it !");
52ExpandedSubBlockEnd.gif        }

53InBlock.gif        else
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55InBlock.gif            Console.BackgroundColor = System.ConsoleColor.Black;
56InBlock.gif            Console.Clear();
57ExpandedSubBlockEnd.gif        }

58InBlock.gif
59ExpandedSubBlockEnd.gif    }

60ExpandedBlockEnd.gif}

61None.gif

        可以用 csc.exe 快速生成上面的代码程序。

        在包含事件声明的类中,声明一个数据类型是委托的这么样的一个对象CustomEventHandler, 它有两个参数(sender和e);在这里使用委托的目的就是在运行中向参数传递方法,并由委托对象生成的实例接收这个参数方法的返回值,因此,在声明委托型的对象时应根据类中的方法结构来定义,或者说在引用类中应当根据委托型对象的结构来生成响应事件的方法结构,比如两者都有哪些类型的参数、返回值的类型,也就是说两者要保持一致。同时,要正确地使用C#中的委托,就必须保持三个步骤:声明——实例化——调用。 
        
        在上面的代码中,EventClass 类就体现了这个原则:
        1. 声明委托类型的对象: public delegate void CustomEventHandler(object sender, EventArgs e);
        2. 创建CustomEventHandler对象的实例CustomEvent:public event CustomEventHandler CustomEvent;
        3. 在InvokeEvent()方法中实现了对该事件的调用,引用事件。

        完了,  就可以在引用类(上面的为TestClass)中像使用windows标准控件一样添加事件并编写事件处理方法了。

        看了一天的委托讲解,才渐渐有点明白,我想大根就是这个意思吧,不知道大家是怎么理解这个JAVA中没有的委托:)

转载于:https://www.cnblogs.com/floatping/archive/2005/09/29/246552.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值