. Net delegation and event Parsing

Source: Internet
Author: User

A few days ago, I used a combination of delegation and multithreading to compile a C # packet capture program. in the coding process, I found that I forgot a lot of things and wrote a lot of redundant code. I have a good combination of delegation and lambda. some information is collected here for future reference. to share with you. this article is not entirely original

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. The first parameter is passed.

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 a lot of webForm user controls. In one scenario, a user control contains a textbox, a button, and a page reference.

The user control page contains a label. 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 delegate 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 about delegated registration. Remember to write it! 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 on the parent page => pageload of the User Control

If not! After the IsPostBack button is clicked, the user control's external variable assignment is executed first (the previously assigned delegate will be empty again, so you need to re-register in the pageload page and cannot write it! IsPostBack ~

3. disadvantages of entrusting only

Although the function has been implemented, we think we can assign a value of null to Puchange. onChangevent on the parent page or change it to another one. I hope we can only add or remove it When referencing it.

This is perfect, so the event comes out ~

2. Use events to re-implement the above Code

1.

2. Parent page

3. Use delegate and event to communicate with winform or Silverlight whenever possible

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

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.