C # 's use of action and Func

Source: Internet
Author: User
Tags ming

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 it is 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.

First, the Commission:

Simulate the scene: Xiao Ming's recent study mood is high, previously bought books have not satisfied the desire to buy Ben (a programmer's 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 analysis

1: 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 void Buybook ();

2: Nearby Bookstore (method of delegation)

Code:

public static void book ()
{
Console.WriteLine ("I am providing books");
}

3: Xiao Ming and the bookstore establish the relationship (to the delegate binding method)

Code:

Buybook Buybook = new Buybook (book);

4: Xiao Ming gives money to get a book (trigger)

Buybook ();

The above content is to better understand the use of the action below I started to explain action and Func

Use of action

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 that we speak of action

New Action (book);            Bookaction ();

Is that a lot easier?

2: Xiao Ming is not satisfied now, I took a programmer's self-cultivation to read, and now want to buy this other books, how to do, I am not to redefine the delegation. In fact, you just need to pass the parameters. Now let's look at the usage of action<t>

  Static 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 there, Action<in t1,in t2>

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); }

Use of Func

Xiao Ming again in doubt, every time I go to the bookstore to get a book, there is no way to send directly to my home, then Func dedicated to provide such services

The Func explanation encapsulates a method that has an indeterminate parameter (perhaps not) but returns the type value specified by the TResult parameter.

1: Let's start by looking at a method that has no parameters but only return values

Static void Main (string[] args)        {            Func<stringnew func<string> (Funcbook);            Console.WriteLine (Retbook);        }          Public Static string Funcbook ()        {            return" sent to book ";        }

2: Method with parameter with return value

Static void Main (string[] args) {Func<string,stringnew func<string ,string>(funcbook); Console.WriteLine (Retbook);}  Public Static string Funcbook (string  bookname) {return  bookname;}

Summarize

1:action for methods that do not return values (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

C # 's use of action and Func

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.