About events and Standard Events in C #1-delegate

Source: Internet
Author: User
When talking about events, we must first talk about delegation! What is delegation? In fact, I cannot make it very clear. Try to explain it as much as possible.
How do we define a method? How to call a method? Think about the following content first. Class Test
{
Static   Void Main ()
{
StringMSG= "Message";
Methodname (MSG );
}
Static   Void Methodname ( String MSG)
{
//Code
}
}

A delegate is a type of reference method. The delegate defines the prototype of the method that it wants to represent, and then it can be associated with any prototype that complies with the method it defines. However, when using delegation, it is like you are calling a method. See how the delegate is defined, instantiated, and used.

Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Leleapplication1
{
/**/ /// <Summary>
///Define a delegate. A delegate is the same as a class. Delegation only represents a method that has been defined as a prototype.
/// </Summary>
Delegate   Void Mydeleg ( String MSG );
Class Program
{
Static   Void Main ( String [] ARGs)
{
// Instantiate a delegate. You can also associate a delegate with a method that you actually want to call.
// The getmessage in new mydeleg (getmessage) is an instance that defines a method. You can also replace getmessage with getmessage2
// The reason will be further explained later.
Mydeleg methodref =   New Mydeleg (getmessage );
Methodref ( " The first message! " );
Console. Read ();
}
Static   Void Getmessage ( String Msginfo)
{
Console. writeline (msginfo );
}
Static   Void Getmessage2 ( String Msginfo2)
{
Console. writeline (msginfo2 );
}
}
Class Testclass
{< br> Public void getmessage ( string MSG)
{< br> console. writeline (MSG);
}
}
}

You will find through the definition of the delegate. Defining a delegate is like defining a method (similar in form ). You can make a comparison.
Define a delegate: Delegate type delegate name (type parameter name, [type parameter]) -- Delegate void mydeleg (string MSG );
Define a method: Type method name (type parameter name, [type parameter]) -- Void getmessage (string msginfo );
Is it similar? Yes. A delegate is actually defining what methods it can represent. If this method conforms to the "prototype" defined by the delegate, we can use the delegate to call that method. That's it.
Why do I need to use delegation? Is it simple to call a method (maybe so simple )? I don't think so. If you run the above method, you will find that you can actually call a method without knowing what the method name is. What is the method? Essentially, I think the method is just a piece of execution Code (No ?), However, once a method is determined, it must be called using the method name. This is not required for delegation. Let's take a look at the example below.Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Leleapplication1
{
/**/ /// <Summary>
///Define a delegate. A delegate is the same as a class. Delegation only represents a method that has been defined as a prototype.
/// </Summary>
Public   Delegate   Void Mydeleg ( String MSG );
/**/ /// <Summary>
///Suppose this is a class that can send information. We can use this class to manage "various information processing".
/// </Summary>
Class Message
{
Public Mydeleg Deleg;
Public   Void Send ( String Message)
{
// It indicates that I have processed the information before sending it.
String Result =   " You have a message: "   + Message;
// Then we need to send this information. Now I am thinking about one thing. How can I send the information? To which?
// When a message is sent to the mobile phone, you need to write the code logic sent to the mobile phone.
// To send information to the mailbox, you need to write the code logic sent to the mailbox here
// To send information to QQ, you need to write the code logic sent to QQ here
// Or send information to the mobile phone and email, QQ, It is the corresponding logic.
// I can also list a lot. The problem you want to solve now is how to send the information to the place the customer needs to send without changing the class.
// My idea is to use delegation.
If (Deleg ! =   Null )
// To call this sentence, you will find that the external defined method is actually referenced here. This gives your method the ability to define the sending logic externally.
Deleg (Message );
}
}
}

The following shows the ability to reference external methods to the internal sending Method for execution.

Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Leleapplication1
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
// Instantiate a message object
Message message =   New Message ();
// Set the member variables of the delegate type in the message object.
Message. Deleg =   New Mydeleg (sendmobilephone );
// When this sentence is called, the logic contained in sendmobilephone (the logic of the sending method) is actually appended to the send method ).
Message. Send ( " I love you " );
Console. Read ();
}
Static   Void Sendmobilephone ( String MSG)
{
//Indicates the logic sent to the mobile phone.
Console. writeline (msg+ "Send to mobilephone");
}
}
 
}

Source Code : Why do you use Delegation?
so I think delegation is a possibility for you to call Methods dynamically. The significance of dynamic calling is that you can insert arbitrary code anywhere, which greatly improves flexibility.

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.