This article is a mirror article of machine translation, please click here to jump to the original article.

View: 29284|Reply: 1

[Source] Introduction to C# delegates (delegate, Action, Func, predicate)

[Copy link]
Posted on 4/9/2018 2:57:15 PM | | |

A delegate is a class that defines the type of method so that it can be passed as an argument to another method. An event is a special commission.

1. Statement of entrustment

(1). delegate

        delegate

   Delegate has at least 0 parameters and a maximum of 32 parameters, and can have no return value or specify a return value type.

   For example, public delegate int MethodtDelegate(int x, int y); Indicates that there are two parameters, and returns an int type.

(2). Action

       Action is a generic delegate with no return value.

Action represents a delegate with no parameters and no return value

Action<int,string> indicates a delegate with an incoming parameter int and no return value for string

  Action<int,string,bool> indicates a delegate with incoming parameters int,string,bool without return values

       Action<int,int,int,int> means that there are 4 int-type parameters passed in and there is no return value

Action has at least 0 parameters and a maximum of 16 parameters, with no return value.

Example:



(3). Func

Func is a generic delegate with a return value

Func<int> means a delegate with no parameters and returns an int value

Func<object,string,int> represents a delegate with the incoming parameter as object and the string returns an int value

Func<object,string,int> represents a delegate with the incoming parameter as object and the string returns an int value

Func<T1,T2,,T3,int> represents a delegate with the incoming parameter T1, T2, ,T3 (generic) returning an int value

Func has at least 0 parameters and a maximum of 16 parameters, which are returned according to the return value generic. There must be a return value, not void


Example:




(4) .predicate

predicate is a generic delegate that returns a bool type

predicate<int> represents a delegate with the incoming parameter int returning bool

Predicate has and only one parameter, and the return value is fixed to bool

Example:







Previous:rabbitMQ Queue Queue Message Persistence [with source code]
Next:Summary of information about ActiveMQ and RabbitMQ
Posted on 12/17/2019 7:42:23 PM |
I have a question. I wrote too much js and got used to callback functions.

Then Func was used as a parameter. And this Func I want to execute asynchronously. If you find some answers, you can put Func in Task.Run
He quickly switched the thread ID.

And I whimsically used Func<string,Task<string>> so I could wait for the function directly, but found that he didn't really switch the thread id.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com