Recommendation 36: Use a delegate declaration in the FCL
There are 3 types of delegate declarations in the FCL, namely: Action, Func, predicate. Especially after their generic versions come out, they have been able to meet most of the requirements of our actual coding process.
- Action means accepting 0 or more input parameters, executing a piece of code without any return value;
- Func means to accept 0 or more input parameters, execute a piece of code, with a return value;
- predicate indicates that a set of criteria is defined and the parameters are interpreted to match the criteria.
There are 17 overloaded versions of action, and overloads with up to 16 parameters.
The overloaded version of Func has 17, and the overload of the maximum parameter has 16 parameters.
Note: The parameters of a few methods can be more than 16, if there is really such a parameter, first of all to consider whether the design of the problem. Alternatively, we can use the params keyword to reduce the argument's declaration. Such as:
static void Method1 (params int[] i)
Indicates that the method accepts 0 or more integer parameters.
Delegate intAddHandler (intIintj); Delegate voidPrinthandler (stringmsg); Static voidMain (string[] args) {AddHandler Add=Add; Printhandler Print=Print; Print (Add (1,2). ToString ()); } Static intADD (intIintj) {returni +J; } Static voidPrint (stringmsg) {Console.WriteLine (msg); }
Delegate declarations AddHandler and Printhandler can be completely superseded by Func and action.
Static voidMain (string[] args) {Func<int,int,int> add =Add; Action<string> Print =Print; Print (Add (1,2). ToString ()); } Static intADD (intIintj) {returni +J; } Static voidPrint (stringmsg) {Console.WriteLine (msg); }
We should be accustomed to using this type of delegate in our code instead of our own delegate declaration.
In addition to handling action, Func, and predicate, there are delegate declarations in the FCL that represent special meanings. Such as
Delegate declaration used to represent the registered event method:
Public Delegate void EventHandler (object sender, EventArgs e); Public Delegate void Eventhandler<teventargs> (object sender, Teventargs e);
Represents a delegate declaration for a thread method:
Public Delegate void ThreadStart (); Public Delegate void Parameterizedthreadstart (object obj);
Delegate declaration that represents the asynchronous callback:
Public Delegate void AsyncCallback (IAsyncResult ar);
Each type of delegate declaration in the FCL represents a special kind of use, although it can be replaced with its own delegate declaration, but it is not necessary to do so, and it will lose the simplicity and standard of the code. Before we implement our own delegation statement, we should first review MSDN and make sure that it is necessary to do so.
Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia
"Go" writing high-quality Code 157 recommendations for improving C # programs--recommendation 36: Using delegate declarations in the FCL