Basic knowledge of C # Fundamentals (21) Delegate (ii)

Source: Internet
Author: User
Looking at the usage of the previous delegate in http://www.php.cn/, we see that

        public void Description (string programername, Descriptiondelegate Description)        {            Description (programername) ;        }

This method when passing a Descriptiondelegate description delegate type comes in, then for this method to make a change, can be used for some convenience, the code is more reasonable. Encapsulate the delegates, and then bring up the programmer's description methods in different languages:
Packaging:

    public class ProgramDescriptionDelegate2    {public        delegate void Descriptiondelegate (string programername);        Public descriptiondelegate mydelegate;        public void Description (string programername)        {            mydelegate (programername);        }    }

The class presented:

    public class TestDescriptionDelegate2    {public        void Csharpprogramer (string programername)        {            Console.WriteLine (String. Format ("{0} Programming with C #!", Programername));        }        public void Cprogramer (string programername)        {            Console.WriteLine (string. Format ("{0} Programming with C language!", Programername));        }        public void Javaprogramer (string programername)        {            Console.WriteLine (string. Format ("{0} programming with the Java language!", Programername));        }        public void Test ()        {            ProgramDescriptionDelegate2 pe = new ProgramDescriptionDelegate2 ();            Pe.mydelegate = Csharpprogramer;            Pe.mydelegate + = Cprogramer;            Pe. Description ("SamYang");        }    }

This is more reasonable, because public void Csharpprogramer (String programername) These methods, should be the user can be defined at any time, can be seen as a change at any time. And ProgramDescriptionDelegate2 is purely a public class, it is only responsible for executing the method bound on the delegate.
Call:

            TestDescriptionDelegate2 t = new TestDescriptionDelegate2 ();            T.test ();

Output:

Come back to see ProgramDescriptionDelegate2 class, public descriptiondelegate mydelegate; should be a field that is encapsulated as a property based on object-oriented encapsulation. Then encapsulate here we want to use a very useful field event.
The package is as follows:

   public class Programerdescriptionevent    {public        delegate void Descriptiondelegate (string programername);        Private event Descriptiondelegate MyEvent;        Public event Descriptiondelegate MyEvent        {            Add {this.myevent + = value;}            Remove {this.myevent-= value;}        }        public void Description (string programername)        {            myevent (programername);        }    }

  public class TestEvent    {public        void Csharpprogramer (string programername)        {            Console.WriteLine ( String. Format ("{0} Programming with C #!", Programername));        }        public void Cprogramer (string programername)        {            Console.WriteLine (string. Format ("{0} Programming with C language!", Programername));        }        public void Javaprogramer (string programername)        {            Console.WriteLine (string. Format ("{0} programming with the Java language!", Programername));        }        public void Test ()        {            programerdescriptionevent pe = new programerdescriptionevent ();            Pe. MyEvent + = Csharpprogramer;            Pe. MyEvent + = Javaprogramer;            Pe. Description ("SamYang");//generally this execution is placed in the place where the event was executed        }    }

Call:

            TestEvent te = new TestEvent ();            Te. Test ();

The results are as follows:

As you can see, what is an event is essentially a delegate, but using an event is a mechanism called an event.
So look at MSDN's description of Delegates and events:
An event is an object that sends a message informing the operation that the operation may have been caused by user interaction (mouse, keyboard action, and so on), or triggered by some other program logic. The object that captures the event and responds to it is called the event receiver.
In event communication, the event sender class does not know which object or method will receive the event it raises. What is needed is a medium between the source and the receiver. The. NET Framework defines a special type (Delegate) that provides functionality for function pointers. A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can only reference methods that match its signature. Thus, the delegate is equivalent to a type-safe function pointer or a callback. Although delegates have many other uses, only the event handling capabilities of delegates are discussed here. A delegate declaration is sufficient to define a delegate class. Declares a signature that provides a delegate, and the common language runtime provides the implementation. For example:
public delegate void AlarmEventHandler (object sender, AlarmEventArgs e);
The delegate keyword notifies the compiler that AlarmEventHandler is a delegate type. By convention, event delegates in the. NET Framework have two parameters: the source that raises the event and the data for the event. That is sender and E.

The above is the basic knowledge of C # basics (21) Commissioned (ii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.