Delegation, events, and applications

Source: Internet
Author: User

Address: http://www.cnblogs.com/wlflovenet/archive/2011/11/02/DelegateStudy.html

 

Because I recently brought two more intern friends ~ They are understanding the delegation. They don't understand what they can do. They don't understand this. Let alone delegate extended Lambda expression events ~ But these are important.
In MVC ef4.1, there will be many shadows of lambda expressions, which must be used in component development user control development events.
Communication between two objects is used to decouple delegate events. The observer mode in the design mode is also an application based on delegate events and the thread WPF in winform development.
SL asynchronous calls and so on all have the shadows of this delegate, so this is very important. It must also be mastered ~ Hope to pass this articleArticleThis gives you a better understanding of delegation and how to use it ~

I. Delegation and extension

Let's take a look at the introduction of msdn ~ ------Delegate

In layman's terms, delegate means that methods can be passed as variables.

1. First write the simplest delegate usage

Static and non-static methods

The result is output.

Hello, wlf

Hello wlf

This is the most primitive Commission

2. evolved to an anonymous method

Is it very troublesome to declare a method after declaring the delegate? If you do not declare the method, you can use the anonymous method to help us see it.CodeA little less ~

3. convert it into a Lambda expression.

Although the above is a lot simpler, we can use lambda expressions more easily ~

This is the evolution of lambda expressions ~ Because there are a lot of lambda expressions in ef linq and so on ~

4. Simplify with action and func

The above code is very simple, but there is another unpleasant point ~ Delegate to be declared! Can I not declare it? Of course ~

First, let's look at msdn to introduce action and func.

In layman's terms, both of these are used to help you declare commission.

The func delegate used to help you declare useless return values is the last parameter with return values.

View ~ In previous versions, we always wanted to declare a helloworld delegate. Now there are only two lines of code, so we can conceal the example of func with returned values. First Parameter
Yes

The second parameter type is the type of the returned value ~ Both functions have 16 reloads ~ So there is no problem with multiple parameters ~

5. Discuss the benefits of delegation

The benefits of delegation the application article has already said a lot. Here we will take the example at the beginning of the article to understand the benefits of delegation.

When we have another language to ask, we only need to add an action instead of modifying the sayhello method. Otherwise, it will be filled with the if else judgment.

6. applications at work

Another important feature of data refresh is available in both LINQ and ef. Where its parameter is that func directly uploads the code to view comments ~

You can easily implement data filtering ~ If you like this function-style programming method, we can introduce the action in the work by the way.

Well, there is a picture with the truth ~ The foreach parameter is exactly the same as the action parameter, so you don't need to be unfamiliar with these new users ~

This function is used to print more than 4 items in the set ~

Tip: Try not to perform set operations in foreach, such as removing elements from the set ~
Otherwise, unexpected results will be displayed ~ The result is 2 4 6.

The reason for the decompilation is that the for index is deleted.

The above topic is far away ~ Regression topic

Ii. Events and Applications

1. Use delegation to implement a common scenario (delegate instead of Event)

In actual work, we use the webform user control in many scenarios. A user control contains a textbox and a button.
A page references

This user control has a label on this page. I want to click the button change in the user control to bind the value in the textbox to the label.
This is a typical delegated event

Application

1. directly add the code to the user control's

Declare a delegate called when a button is clicked

2. Code for referencing the page of a user control

Of course, the lambda expression can be fully written.

Here we mainly talk aboutRemember to write the delegated registration! Out of ispostback

The approximate order is as follows: first execute the user control's external variable assignment => then execute the parent page's external variable assignment (not in this example) =>
Then execute pageload => pageload of the user control on the parent page.

If you do not write! Out of ispostbackClick the button and execute it first.
The user control's external variable assignment (the previously assigned delegate will be empty again, so you need to re-register in the page pageload and cannot write it! Ispostback ~

3. disadvantages of entrusting only

Although the function is implemented, we want to assign a value of null to puchange. onchangevent on the parent page or change it to another one.
However, I hope that we can only add or remove

This is perfect, so the event comes out ~

2. Use events to re-implement the above Code

1.

2. Parent page

3. Use delegates and events in winform or Silverlight whenever possible
Laicommunication

In this way, the coupling is better reduced, avoiding the parent form object in the child form.

Iii. Other entrusted Materials

Commissioned inverter and covariant

When to use delegation and when to use interfaces

Regular Expression Delegation

Asynchronous delegated Programming

Overview of event-based asynchronous mode

Iv. Summary

This article is a beginner. I hope that new users can have a better understanding of delegated events and be able to skillfully use delegated extensions in their work to simplify code ~
And be able to skillfully use events ~ The purpose of this article has also been achieved

Because I recently brought two more intern friends ~ They are understanding the delegation. They don't understand what they can do. They don't understand this. Let alone delegate extended Lambda expression events ~ But these are important.
In MVC ef4.1, there will be many shadows of lambda expressions, which must be used in component development user control development events.
Communication between two objects is used to decouple delegate events. The observer mode in the design mode is also an application based on delegate events and the thread WPF in winform development.
SL asynchronous calls and so on all have the shadows of this delegate, so this is very important. It must also be mastered ~ I hope this article will help you better understand the delegation and how to use it ~

I. Delegation and extension

Let's take a look at the introduction of msdn ~ ------Delegate

In layman's terms, delegate means that methods can be passed as variables.

1. First write the simplest delegate usage

Static and non-static methods

The result is output.

Hello, wlf

Hello wlf

This is the most primitive Commission

2. evolved to an anonymous method

Isn't it very troublesome to declare the method after declaring the delegate? If you don't declare the method, you can help us see that the code is much reduced ~

3. convert it into a Lambda expression.

Although the above is a lot simpler, we can use lambda expressions more easily ~

This is the evolution of lambda expressions ~ Because there are a lot of lambda expressions in ef linq and so on ~

4. Simplify with action and func

The above code is very simple, but there is another unpleasant point ~ Delegate to be declared! Can I not declare it? Of course ~

First, let's look at msdn to introduce action and func.

In layman's terms, both of these are used to help you declare commission.

The func delegate used to help you declare useless return values is the last parameter with return values.

View ~ In previous versions, we always wanted to declare a helloworld delegate. Now there are only two lines of code, so we can conceal the example of func with returned values. First Parameter
Yes

The second parameter type is the type of the returned value ~ Both functions have 16 reloads ~ So there is no problem with multiple parameters ~

5. Discuss the benefits of delegation

The benefits of delegation the application article has already said a lot. Here we will take the example at the beginning of the article to understand the benefits of delegation.

When we have another language to ask, we only need to add an action instead of modifying the sayhello method. Otherwise, it will be filled with the if else judgment.

6. applications at work

Another important feature of data refresh is available in both LINQ and ef. Where its parameter is that func directly uploads the code to view comments ~

You can easily implement data filtering ~ If you like this function-style programming method, we can introduce the action in the work by the way.

Well, there is a picture with the truth ~ The foreach parameter is exactly the same as the action parameter, so you don't need to be unfamiliar with these new users ~

This function is used to print more than 4 items in the set ~

Tip: Try not to perform set operations in foreach, such as removing elements from the set ~
Otherwise, unexpected results will be displayed ~ The result is 2 4 6.

The reason for the decompilation is that the for index is deleted.

The above topic is far away ~ Regression topic

Ii. Events and Applications

1. Use delegation to implement a common scenario (delegate instead of Event)

In actual work, we use the webform user control in many scenarios. A user control contains a textbox and a button.
A page references

This user control has a label on this page. I want to click the button change in the user control to bind the value in the textbox to the label.
This is a typical delegated event

Application

1. directly add the code to the user control's

Declare a delegate called when a button is clicked

2. Code for referencing the page of a user control

Of course, the lambda expression can be fully written.

Here we mainly talk aboutRemember to write the delegated registration! Out of ispostback

The approximate order is as follows: first execute the user control's external variable assignment => then execute the parent page's external variable assignment (not in this example) =>
Then execute pageload => pageload of the user control on the parent page.

If you do not write! Out of ispostbackClick the button and execute it first.
The user control's external variable assignment (the previously assigned delegate will be empty again, so you need to re-register in the page pageload and cannot write it! Ispostback ~

3. disadvantages of entrusting only

Although the function is implemented, we want to assign a value of null to puchange. onchangevent on the parent page or change it to another one.
However, I hope that we can only add or remove

This is perfect, so the event comes out ~

2. Use events to re-implement the above Code

1.

2. Parent page

3. Use delegates and events in winform or Silverlight whenever possible
Laicommunication

In this way, the coupling is better reduced, avoiding the parent form object in the child form.

Iii. Other entrusted Materials

Commissioned inverter and covariant

When to use delegation and when to use interfaces

Regular Expression Delegation

Asynchronous delegated Programming

Overview of event-based asynchronous mode

Iv. Summary

This article is a beginner. I hope that new users can have a better understanding of delegated events and be able to skillfully use delegated extensions in their work to simplify code ~
And be able to skillfully use events ~ The purpose of this article has also been achieved

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.