C # the usage of Action and Func,

Source: Internet
Author: User

C # the usage of Action and Func,

In the past, I used to define a delegate to write the delegate, but recently I have seen that some foreigners write the source code in the action and func modes, at that time, I felt unfamiliar with the source code, so I took the time to learn the two methods and found that the code was indeed concise. We can also use these two methods in practice.

Let's talk about delegation:

Simulate the scenario: James has a high learning mood recently. He has never been able to satisfy his desire to buy books (a programmer's self-cultivation ). But in the past, I used to run a book factory to buy it. nm was too far away to handle it. I went to a nearby bookstore to buy it. James went to give the money and got a book back. This process was commissioned. Start Analysis

1: James wants to buy a self-developed programmer book (xx books won't be bought). Hard requirements (this is to define the nature of delegation)

Code:

Private delegate void BuyBook ();

2: nearby bookstore (delegated method)

Code:

Public static void Book ()
{
Console. WriteLine ("I provide books ");
}

3: Relationship between Xiao Ming and Bookstore (delegate binding method)

Code:

BuyBook buybook = new BuyBook (Book );

4: James give money to get a book (trigger)

Buybook ();

The above content is to understand the delegate usage. Next I will explain the actions and Func

Action usage

1: James is very upset. I just bought a book. Every time I define it, I get bored. Is there a way not to define the Commission, the Action we are talking about today

Action BookAction = new Action(Book);            BookAction();

Is this much simpler?

2: James is not satisfied now. I have finished reading a programmer's self-cultivation. Now I want to buy another book. What should I do? Should I re-define the Commission. In fact, you don't need to pass through the parameters. Next, let's take a look at the usage of Action <T>.

Static void Main (string [] args) {Action <string> BookAction = new Action <string> (Book); BookAction ("Hundred Years of loneliness ");} public static void Book (string BookName) {Console. writeLine ("I bought books: {0}", BookName );}

3: Now James has changed his mind. Not only do I have to choose books myself, but I have to buy books from a great book manufacturer. Is there any way to do this, action <in T1, in T2>

 

Static void Main (string [] args) {Action <string, string> BookAction = new Action <string, string> (Book); BookAction ("Hundred Years of loneliness ", "Beijing Bookstore");} public static void Book (string BookName, string ChangJia) {Console. writeLine ("I bought books: {0} from {1}", BookName, ChangJia );}

Func usage

James has another question. Every time I go to the bookstore to get a book, is there a way to send it directly to my house? Then Func provides such a service?

Func encapsulates a method that may not have parameters (maybe not) but returns the type value specified by the TResult parameter.

1: Let's first look at a method with no parameters but return values.

Static void Main (string [] args) {Func <string> RetBook = new Func <string> (FuncBook); Console. writeLine (RetBook);} public static string FuncBook () {return "sent a book ";}

2: Methods with parameters and return values

static void Main(string[] args){Func<string,string> RetBook = new Func<string,string>(FuncBook);Console.WriteLine(RetBook);}public static string FuncBook(string BookName){return BookName;}

3: A very important use of Func is to pass the value. Here is a simple code to describe it.

Func <string> funcValue = delegate {return "I am the value to be passed 3" ;}; DisPlayValue (funcValue );

Note 1: DisplayVaue refers to the processing of incoming values, such as cache processing, or unified addition of databases.

Private static void DisPlayValue (Func <string> func) {string RetFunc = func (); Console. writeLine ("I'm testing the passed value: {0}", RetFunc );}

Summary

1: Action is used for methods without return values (parameters can be passed as needed)

2: The opposite of Func is used for methods with returned values (the same parameter is based on your own situation)

3: Remember to use action if there is no return, and Func if there is a return

 

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.