Use of C#action and Func

Source: Internet
Author: User

I used to define a delegate to write the Commission, but recently read some foreigners write the source code is to write with action and Func, at that time feel very strange to see the Origin code also feel unfamiliar, so I took the time to learn the two ways, And then found that the code is quite concise. These two ways we can also go to practice the process to use slowly. Let's talk about it. Commissioned: A simulation of the scene: Xiao Ming's recent study mood high, previously bought books have not satisfied the desire to buy Ben (a programmer self-cultivation). However, before always run the book factory to buy, NM, too far to carry, went to the nearby bookstore to buy, Xiao Ming to give money to get a book back, this process is entrusted. Start Analysis1: Xiao Ming to buy a programmer self-cultivation of books (XX books do not buy) hard requirements (this is to define the nature of the delegate) code:Private Delegate voidBuybook ();2: Nearby Bookstore (delegate method) code: Public Static voidBook () {Console.WriteLine ("I'm a book provider.");}3: Xiao Ming and the bookstore establish a relationship (to a delegate binding method) code: Buybook Buybook=NewBuybook (book);4: Xiao Ming gives money to book (trigger) Buybook (); The above is to understand the usage of the delegate. I started to explain the use of action and funcaction.1: Xiao Ming is very distressed, I just buy a book, every time Let me define, bored to death, there is no way to define a delegate, then there is, there is, is we speak of Actionaction bookaction=NewAction (book); Bookaction (); That's a lot easier.2: Xiao Ming is not satisfied now, I took a programmer's self-accomplishment, and now I want to buy this other book, How to do, I do not have to redefine the delegation. In fact, you just need to pass the parameters. Now let's see action<t>.usage ofStatic voidMain (string[] args) {Action<string> bookaction =Newaction<string>(book); Bookaction ("Hundred Years of Loneliness"); }         Public Static voidBook (stringbookname) {Console.WriteLine ("I was buying a book: {0}", BookName); }3: Now Xiaoming changed his mind, I not only to choose their own books, I also want to buy in a good book manufacturers, there is no such way, then tell you have,action<inchT1,inchT2>Static voidMain (string[] args) {Action<string,string> bookaction =Newaction<string,string>(book); Bookaction ("Hundred Years of Loneliness","Beijing Grand Bookstore"); }         Public Static voidBook (stringBookName,stringChangjia) {Console.WriteLine ("I'm buying a book: {0} from {1}", Bookname,changjia); }func the use of Xiao Ming again in question, every time I go to the bookstore to get books, there is no way to send directly to my home, then Func specifically provides such a service Func explains encapsulating a method that has parameters (perhaps not) but returns the type value specified by the TResult parameter. 1: Let's look at a method that has no parameters but only return valuesStatic voidMain (string[] args) {Func<string> Retbook =Newfunc<string>(Funcbook);        Console.WriteLine (Retbook); }         Public Static stringFuncbook () {return "Here comes the book."; }2: A method with a parameter that returns a valueStatic voidMain (string[] args) {Func<string,string> Retbook =Newfunc<string,string>(Funcbook); Console.WriteLine (Retbook ("AAA"));} Public Static stringFuncbook (stringbookname) {returnBookName;}3: Func A very important use is to pass the value, below I give a simple code to illustrate the Func<string> Funcvalue =Delegate            {                return "I was about to pass the value 3";            }; Displayvalue (funcvalue); note 1:displayvaue is the processing of incoming values, the processing of the metaphor cache, or the uniform addition of databases, etc.Private Static voidDisplayvalue (func<string>func) {            stringRetfunc =func (); Console.WriteLine ("I'm testing it. Value passed: {0}", Retfunc); } Summary1: Action is used for methods that do not return a value (parameters can be passed according to their own circumstances)2: Func is the opposite of a method that has a return value (same parameter according to its own condition)3: Remember to use action with no return, use Func to return

Use of C#action and Func

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.