What is the purpose of a function when passing parameters ??

Source: Internet
Author: User

Recently, I spoke to a colleague about LINQ. I mentioned that many of the methods in LINQ seem to have turned out to be a method that is not commonly used in the past. This is to use a function as a parameter, and my colleagues may not understand it, or in this case, what is the purpose of work.

Here are two simple and common usage examples.

1. Fill in multiple execl to repeat the code.

The idea here is relatively simple, and it is easy to think that when I export a few pieces of execl, the corresponding three steps will be opened, filled with execl, closed and saved execl.

But in these execl files, open execl and close to save execl. Our code workers know that we should not write repeated code. So good, encapsulate it .........

But the key is to fill execl in different steps. How to do this? (Don't Tell Me To encapsulate multiple modes with the design mode, and then fill them with different modes,

Because the padding for execl is different, that is, the worksheet table to be filled is different. Here we can pass the function as a parameter as follows.

Public string OnAction(params Action<Worksheet>[] actions)

{

// Open execl

For (INT I = 0; I <actions. length; I ++)

{

Wsheet = myexecl. worksheets. getitem (I + 1 );

// Fill in the corresponding execl table worksheet here. As for how to fill it, it depends on the implementation of your corresponding action [I] method.

Action [I] (wsheet );

}

// Close and save execl

}

2.ASP. NETThe application of the write generic delegate in the project.

A scenario is presented here.

Specific scenarios:When the client calls the WCF Service, we need to handle exceptions in the client. The most common error exceptions are communicationexception, timeoutexception, and exception.

Generally, we can only execute this method and capture exceptions. there is nothing to do with one or two methods. what will happen if we reach 10, not to mention 100. all of your projects are exactly the same try {} catch {}.

Fortunately, the author gave us a solution.

Consider a method as an object. I only consider exceptions in method execution. What do you do? I don't care.

Public static void invoke <tcontract> (tcontract proxy, Action <tcontract> action)
{

Try {

Action (proxy ));

}

Catch ()

{

}

Catch ()

{

}

}

In this way, the whole world is quiet.

 

PS: To be honest, the theoretical explanation is very troublesome. I only need more examples. If you have any misunderstandings, please point out. Thank you.

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.