Article 07th: C # example of delegate

Source: Internet
Author: User
Tags first string

Concept

Delegate is the delegate. If you have used a callback function in C ++ or js, it is easier to understand. Callback functions are often used in asynchronous operations. For example, in Ajax, when HttpRequest executes an asynchronous request, a callback function is required when the status changes. A callback function is actually a method call pointer, also known as a function pointer. In C #, delegate is used to transmit methods as parameters.

 

Simple delegation example

Define a delegate. The Code is as follows:

Private delegate string GetString ();

This delegate is used to obtain a string. Next, call the Main method:

Static void Main (String [] args)

{

Int I = 20;

// Pass the ToString method of I as a parameter to the Delegate

GetString getString = new GetString (I. ToString );

Console. WriteLine (getString (); // execute the delegate

}

This is the simplest example of delegation. The reason for this simple example is to let everyone know that delegation is not unfathomable.

 

Multicast Delegation

The so-called multicast delegate is actually the addition of multiple methods through + and + =, And the subtraction of methods can also be achieved through-and-=. In Windows form programming, we can add a click event for a button to see this Code:

This. button1.Click + = new System. EventHandler (this. button#click );

With multicast delegation, we can add more processing functions for Click events. The Code is as follows:

This. button1.Click + = new System. EventHandler (this. button#click1 );

 

To delete a method, use the following code:

This. button1.Click-= new System. EventHandler (this. button#click );

 

Anonymous Method

The anonymous method is a code block used as the delegate parameter. That is to say, you do not need to name the method passed to the delegate. You only need to write the code executed by the delegate in a specified code block. The following code delegates an anonymous method:

// Define this delegate. the return value of the method to be executed is void and the parameter is of the string type.

Private delegate void ShowString (string Text );

 

Static void Main (String [] args)

{

String str = "this is an anonymous method ";

 

ShowString ss = delegate (string Text)

{

Console. WriteLine (Text );

};

WriteString ws = new WriteString ();

Ss + = new ShowString (ws. write );

Ss. Invoke (str );

}

 

LambdaExpression

C #3.0 provides a new syntax for anonymous methods: Lambda expressions. Lambda can be used for delegate type. For more information, see Lambda expressions in MSDN (C # programming guide)

The following is an example of Lambda:

// Define this delegate. the return value of the method to be executed is void and the parameter is of the string type.

Private delegate void ShowString (string Text1, string Text2 );

 

Static void Main (String [] args)

{

String str1 = "first string ";

String str2 = "second string ";

 

// λ operator> = List the parameters required by the delegate on the left. If it is a parameter, You can omit ()

ShowString ss = (Text1, Text2) =>

{

// The right side of the operator is a method or an anonymous method. If there is only one row of the method, you can omit it {}

Console. WriteLine (Text1 + "\ n" + Text2 );

};

 

Ss (str1, str2 );

}

 

 

Conclusion

Finally, let's look at the old saying: If you want to learn more, you should use more and check more. Practice is the only way to success. At the end of the document, download the example sampledelegatetest.rar.

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.