Differences between events and delegation, delegation, and events-constantly updated

Source: Internet
Author: User

01. Simple Delegation

What information does the delegate need to carry? First, it stores the method name, the parameter list (method signature), and the returned type. For example: Delegate string/* return type */processdelegate (int I); this is the definition of a delegate. The blue part is the keyword for declaring the delegate, the red part is the returned type, and the black part is the delegate type name, which is similar to a class name, while the () part is the parameter part. It means that if you want to use this delegate to do things, the method of doing things must meet the following conditions: 1. The return type is the same as the return type of the delegate, here is the string type; 2. It can have only one parameter and is of the int type.

OK. If the above two conditions are met, everything can work :)

Using system;

Using system. Collections. Generic;
Using system. text;

Namespace testapp
{
/// <Summary>
/// Delegate
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public Delegate string processdelegate (string S1, string S2 );

Class Program
{
Static void main (string [] ARGs)
{
/* Call Method */
Processdelegate Pd = new processdelegate (new test (). process );
Console. writeline (Pd ("text1", "text2 "));
}
}

Public class test
{
/// <Summary>
/// Method
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public String process (string S1, string S2)
{
Return S1 + S2;
}
}
}

02. Generic Delegation

Using system;

Using system. Collections. Generic;
Using system. text;

Namespace testapp
{
/// <Summary>
/// Delegate
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public Delegate string processdelegate <t, s> (T S1, s S2 );

Class Program
{
Static void main (string [] ARGs)
{
/* Call Method */
Processdelegate <string, int> Pd = new processdelegate <string, int> (new test (). process );
Console. writeline (Pd ("text1", 100 ));
}
}

Public class test
{
/// <Summary>
/// Method
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public String process (string S1, int S2)
{
Return S1 + S2;
}
}
}

03. Callback Method

Using system;

Using system. Collections. Generic;
Using system. text;

Namespace testapp
{
/// <Summary>
/// Delegate
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public Delegate string processdelegate (string S1, string S2 );

Class Program
{
Static void main (string [] ARGs)
{
/* Call Method */
Test T = new test ();
String R1 = T. Process ("text1", "text2", new processdelegate (T. process1 ));
String r2 = T. Process ("text1", "text2", new processdelegate (T. process2 ));
String R3 = T. Process ("text1", "text2", new processdelegate (T. process3 ));

Console. writeline (R1 );
Console. writeline (R2 );
Console. writeline (r3 );
}
}

Public class test
{
Public String process (string S1, string S2, processdelegate process)
{
Return Process (S1, S2 );
}

Public String process1 (string S1, string S2)
{
Return S1 + S2;
}

Public String process2 (string S1, string S2)
{
Return S1 + environment. newline + S2;
}

Public String process3 (string S1, string S2)
{
Return S2 + S1;
}
}
}

 

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.