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 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 is to understand the usage of the delegate. 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


Action bookaction = 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 void Main(string[] args)
{
      Action<string> BookAction = new Action<string>(Book);
      BookAction ("100 years of loneliness");
}
Public static void Book(string BookName)
{
      Console.WriteLine ("I bought the book is: {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<in T1,in T2>static void Main(string[] args)
{
      Action<string,string> BookAction = new Action<string,string>(Book);
      BookAction ("100 years of loneliness", "Beijing Big Bookstore");
}
Public static void Book(string BookName,string ChangJia)
{
      Console.WriteLine ("I bought the book is: {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<string> RetBook = new Func<string>(FuncBook);
      Console.WriteLine(RetBook);
}
Public static string FuncBook()
{
      Return "Send a book"
}


2: Method with parameter with return value


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


3:func a very important use is to pass the value, below I give a simple code to illustrate


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


Note 1:displayvaue is the processing of incoming values, metaphor of cache processing, or uniform addition of databases, etc.


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


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



Original address: https://www.cnblogs.com/xuxiaoshuan/p/6844511.html



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.