C # event Delegate __c#

Source: Internet
Author: User
Code is understood by the mother   Direct open dry   
Using System;
Using System.Collections;
public delegate void addEventHandler (object sender, EventArgs e);  Declares an event delegate type public
class Listwithevent:arraylist
{public
    event addEventHandler Added;    Declares an event
    protected virtual void onchanged (EventArgs e)
    {
        if (Added!= null)
            Added (this, e);
    public override int Add (object value)
    {
        int i = base. ADD (value);
        OnChanged (eventargs.empty);
        return i;
    }
}
Class Eventmethod
{public
    void ListChanged (object sender, EventArgs e)
    {
        Console.WriteLine (" This is called when the event fires. ");
    }
Class program
{public
    static void Main ()
    {
        listwithevent List = new Listwithevent ();
        Eventmethod test = new Eventmethod ();
        list.added + = new addEventHandler (test. ListChanged);
        List.add ("Item 1");
        List.add ("Item 2");
        Console.read ();
    }



Do you understand me?

No matter, go on.


Using System;
Class Publisher
{public
    delegate void Publish ()//modifier  keyword    type   delegate name public
    event Publish Onpublish;   Defines the Pubish delegate type event public
    void issue ()     //define the method that triggers the event
    {
        if (onpublish!= null)        //event is not empty
        {
            Console.WriteLine ("Issue publication"). ");
            Onpublish ();             Event occurrence
        }
    }
class subscriber
{public
    void Receive ()
    {
        Console.WriteLine (The Subscriber has received the publication.) ");
    }
}
Class Story
{
    static void Main ()
    {
        Publisher Pub = new publisher ();
        Subscriber Sub = new Subscriber ();
        Pub.onpublish + = new Publisher.publish (sub.receive); A new delegate object (that is, an event) is stored in the object-> event of the delegate at this time, the event is not NULL, and the parameter is the method of the target object
        pub.issue ();   Trigger event                                               
        Console.read ();
    }



Wow, haha. It's okay

Come on.

Using System;
    Class Publisher//publisher {public delegate void Pubcomputer (string magazinename);
    public delegate void Publife (string magzinename);
    public event Pubcomputer Onpubcomputer;
    public event Publife Onpublife; public void Issuecomputer ()//define the method that triggers the event {if (Onpubcomputer!= null) {CONSOLE.WR Iteline ("The issue of Computer" magazine.)
            ");   Onpubcomputer ("Computer magazine");
        Event takes place with delegate pass parameter}} public void Issurelife ()//define the method that triggers the event {if (Onpublife!= null) {Console.WriteLine ("Published Life" magazine.)
            ");   Onpublife ("Life magazine");
    Event occurrence}} class Subscriber//Subscriber {private string name;
    Public Subscriber (string name) {this.name = name;
    public void Receive (string magazinename) {Console.WriteLine (name + "received" + magazinename);
        } class Story {static void Main () {publisher Pub = new publisher (); Subscriber ZS = new Subscriber ("John"); Pub.onpubcomputer + = new Publisher.pubcomputer (ZS. Receive);
        Book an event to the event Publisher subscriber ls = new Subscriber ("Dick"); Pub.onpubcomputer + = new Publisher.pubcomputer (ls.  Receive); Triggers the event to cause the associated method Pub.onpublife = = new Publisher.publife (ls. Receive); The//new operator is the object that creates a delegate
                            A new delegate object (that is, an event) is placed in the object-> event of the delegate, associated with the target object method, at which point the event is not empty  onpublife. =null
Pub.issuecomputer (); Trigger event Pub.issurelife (); Console.WriteLine (); Console.WriteLine ("One year later"); Pub.onpubcomputer-= new Publisher.pubcomputer (ls. Receive); Pub.issuecomputer (); Pub.issurelife (); Console.read (); }}



It's better now.


Another big trick ...

Using System;
Using System.Collections;
    Class Pubeventargs:eventargs {private readonly string m_magazinename;
    Private readonly string m_pubdate;
        Public Pubeventargs (String magazinename, DateTime pubdate) {m_magazinename = Magazinename;
        pubdate = new DateTime ();
    M_pubdate = Pubdate.tostring ();
        public string Magazinename//property {get {return m_magazinename;
        } public string pubdate//property {get {return m_pubdate;
    Class Publisher {public delegate void Pubcomputereventhandler (object sender, Pubeventargs e);     
    public delegate void Publifeeventhandler (object sender, Pubeventargs e);         
    public event Pubcomputereventhandler Pubcomputer;                         
    public event Publifeeventhandler Publife;                                      protected virtual void Onpubcomputer (Pubeventargs e) {                            
        Pubcomputereventhandler handler = Pubcomputer;                                                                          
            Prevents a possible thread synchronization problem if (handler!= null)//When the event bindings the corresponding method, the event is non-null {   Handler (this, e);                                                                                    
    This is Object E of the class that triggered the event is an object of the class that contains the event arguments } protected VI  rtual void Onpublife (Pubeventargs e) {Publifeeventhandler handler = Publife;
        With the same if (handler!= null) {handler (this, e); } public void Issuecomputer (string magazinename, DateTime pubdate) {Console.WriteLine ("Release" + Magazi
        Nename);
    Onpubcomputer (New Pubeventargs (Magazinename, pubdate)); public void Issuelife (string magazinename, DateTime pubdate) {CONSOLE.WRiteline ("release" + Magazinename);
    Onpublife (New Pubeventargs (Magazinename, pubdate));
    } class Subscriber {private string name;
    Public Subscriber (string name) {this.name = name; public void receive (object sender, Pubeventargs e) {Console.WriteLine (e.pubdate + "" + Name + "received" +
    E.magazinename);
        } class Story {static void Main () {publisher Pub = new publisher ();
        Subscriber ZS = new Subscriber ("John"); Pub.pubcomputer + = new Publisher.pubcomputereventhandler (ZS.
        Receive);
        Subscriber ls = new Subscriber ("Dick"); Pub.pubcomputer + = new Publisher.pubcomputereventhandler (ls.
        Receive); Pub.publife + = new Publisher.publifeeventhandler (ls.
        Receive);
        Pub.issuecomputer ("Computer Magazine", Convert.todatetime ("2010-9-15"));
        Pub.issuelife ("Life magazine", Convert.todatetime ("2010-9-15"));
        Console.WriteLine ();
        Console.WriteLine ("One year later"); Pub.pubcomputer-= new Publisher.Pubcomputereventhandler (LS.
        Receive);
        Pub.issuecomputer ("Computer Magazine", Convert.todatetime ("2011-9-15"));
        Pub.issuelife ("Life magazine", Convert.todatetime ("2011-9-15"));
    Console.read ();
 }
}



The. NET Framework is named 1, the name of the event should use Pascal casing naming method
2 when declaring delegate, you must use the void type as the return value. Event naming aspects For example: the event delegate for the EventName event is
Eventnameeventhandler, time to accept two incoming parameters, all named sender and E.
3 defines a class that provides event data, names classes with Eventnameeventargs, derives the class from System.EventArgs, and then adds all event-specific members
4 Implement the time criterion in the class. If the event does not require passing arguments, also declare two arguments, E
Parameter directly uses the System.EventArgs class provided by the system. If you pass the data, you inherit a class from the System.EventArgs and put the data inside.
5 provide a protected method in the class that raises the event. Named in OnEventName, which raises an event in the method.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.