A brief introduction and usage of C#'s Action and Func

  I used to write delegates by defining a delegate, but recently I saw that the source code written by some foreigners was written in the form of action and func. Take time to learn these two methods, and then find that the code is indeed much simpler. We can also use these two methods slowly in the process of practice.

Let me talk about the commission first :

Let's simulate the scene: Xiao Ming's learning mood is high recently, and the books he bought before can't satisfy his desire, so he plans to buy a book (a programmer's self-cultivation). But in the past, I always went to the book factory to buy, nm, I couldn’t carry it too far, so I went to the nearby bookstore to buy, Xiao Ming went to give the money and got a book back, this process is entrusted. start analysis

1: Xiaoming wants to buy a book on self-cultivation of a programmer (xx book will not be bought) mandatory requirements (this is to define the nature of entrustment)

code:

 private delegate void BuyBook();

2: Nearby bookstores (method of entrusting)

code:

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

3: Xiaoming establishes a relationship with the bookstore (binding method to entrustment) 

code:

  BuyBook buybook = new BuyBook(Book);

4: Xiao Ming gives money to get books (trigger)

buybook();

The above content is to understand the usage of the delegate. Next, I will start to explain Action and Func.

Action usage

1: Xiao Ming is very distressed. I just bought a book and asked me to define it every time. I am so annoyed. Is there a way not to define the entrustment? Is there a way? It really is. It is the Action we talked about today.

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

Is this much simpler?

2: Xiao Ming is dissatisfied now. I have finished reading A Programmer’s Self-cultivation, and now I want to buy another book. What should I do? Do I want to redefine the commission again. In fact, you don’t need to just pass the parameters through. Let's look at the usage of Action<T>

  static void Main(string[] args)
        {
            Action<string> BookAction = new Action<string>(Book);
            BookAction("百年孤独");
        }
        public static void Book(string BookName)
        {
            Console.WriteLine("我是买书的是:{0}",BookName);
        }

3: Now Xiao Ming has changed his mind again. Not only do I have to choose books by myself, but I also want to buy them from an awesome book manufacturer. Is there such a way? Let me tell you, Action<in T1,in T2>

static void Main(string[] args)
        {
            Action<string,string> BookAction = new Action<string,string>(Book);
            BookAction("百年孤独","北京大书店");
        }
        public static void Book(string BookName,string ChangJia)
        {
            Console.WriteLine("我是买书的是:{0}来自{1}",BookName,ChangJia);
        }

 Usage of Func

Xiao Ming has a question again. Every time I go to the bookstore to pick up books, is there a way to deliver them directly to my home? Then Func specially provides such a service

Func Explanation Encapsulates a method that may or may not have parameters (maybe none) but returns a value of the type specified by the TResult parameter.

1: Let's first look at a method that has no parameters and only returns a value

static void Main(string[] args)
        {
            Func<string> RetBook = new Func<string>(FuncBook);
            Console.WriteLine(RetBook);
        }
        public static string FuncBook()
        {
            return "送书来了";
        }

2: A method with parameters and a 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: A very important use of Func is to pass values. Let me give a simple code to illustrate

  Func<string> funcValue = delegate
            {
                return "我是即将传递的值3";
            };
            DisPlayValue(funcValue);

Note 1: DisplayVauue is to process the incoming value, which is a metaphor for cache processing, or uniformly adding databases, etc.

 private static void DisPlayValue(Func<string> func)
        {
            string RetFunc = func();
            Console.WriteLine("我在测试一下传过来值:{0}",RetFunc);
        }

Summarize

1: Action is used for methods with no return value (parameters can be passed according to your own situation)

2: Func is just the opposite for methods with return values ​​(the same parameters are based on their own circumstances)

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

Reprint: https://www.cnblogs.com/LipeiNet/p/4694225.html

Guess you like

Origin blog.csdn.net/weixin_42565127/article/details/130947237
Recommended