C # delegate introduction (delegate, Action, Func, predicate ),A delegate is a class that defines the type of a method so that the method can be passed as a parameter of another method. An event is a special delegate.
1. Delegate statement
(1).
Asp Tutorial: Delegate, Action, Func, and Predicate in. net Development
Many new programs, such as Linq and Lamda expressions, are poorly written. The use and differences of related Delegate, Action, Func, and Predicate are often confusing. Here we
We usually declare a delegate type if we want to use a delegate, for example: Private Delegate string Say ();The string describes the return type of the method that applies to the delegate, which has no arguments after the delegate name Say ,
After this article is written, I found that there are a lot of discussions on expresstion and func on the Internet. I don't want to check them. I have read these articles in the same way, but I am still going deeper:
C # Use of Action and Func,
In the daily use of delegate, we usually need to display the declaration of a delegate named XXX, and when using the Action delegate, do not display and define a delegate that encapsulates the process without
It's always been a bit confusing for events and delegates, and it's not going to work. Find a time, summed up a bit, feel a lot clearer.Let's start with a personal understanding of the conclusion that: delegate is a type in C # that is actually a
C # initially had only delegate, and later versions encapsulated action, func, eventhandlerAbout ActionActually, action is equivalent to the old version. Public Delegate voidAction (); Public Delegate voidAction(T1 arg1); Public Delegate
Delegation plays an extremely important role in C # programming. A delegate can encapsulate a function into a delegate object, and multiple delegates can be combined into one delegate, the delegate object can be stored and transferred like a normal
Idle in the morning, see action OptimizationCodeSmall StructureArticleAnd it is much more readable and scalable than normal if... else or switch... case.
Here, I will use other people's information to learn about it again.
From the initial
The following content is compiled by Yang Guo under the bodhi tree according to Microsoft msdn. Please indicate the source of the post. Msdn official explanation of func :Encapsulate a method that has a parameter and returns the type value specified
Generally, if we want to use a delegate, we generally declare a delegate type first, for example:
private delegate string Say();
String indicates that the return type of the delegate method is string, and the delegate nameSayThere is no parameter
Action
Action
Func
Func
Action: encapsulates a method that does not have parameters and does not return a valuepublic delegate void Action ()The action:action generic implements the definition of 1 to 16 incoming parameters, but
Transfer from http://www.cnblogs.com/LittleFeiHu/p/4035166.htmlAction: Encapsulates a method that does not have parameters and does not return a value Public Delegate voidAction () Action: The generic for action implements the definition of 1 to 16
Let's start with a personal understanding of the conclusion that:
Delegate is a type in C # that is actually a class that holds a reference to a method.
There is no essential difference between the variable declared by delegate and the
C # system delegate Action And Func,
Action: encapsulate a method that does not have parameters and does not return values.
public delegate void Action()
Action : the generic type of Action defines 1 to 16 input parameters, but there is still no
I. FuncFunc is a. NET built-in delegate.Func,func is a. NET built-in generic delegates.
Func
Func
Func
Func
Func
It has 5 forms, except that the number of parameters is different; the first one is no parameter, but
Description:
Encapsulate a method that has a parameter and returns the type value specified by the tresult parameter.
Syntax:
Public DelegateTresultFunc (T Arg );
Parameter type:
T: parameter type of the
A small example using func Delegation
// Create an integer array int [] intarray = new int [] {0, 1, 2, 3}; // declare the func delegate to determine whether it is an odd number of func isodd = I => (I & 1) = 1); // run the query operation. Do not
Delegation plays an extremely important role in C # programming. A delegate can encapsulate a function into a delegate object, and multiple delegates can be combined into one delegate, the delegate object can be stored and transferred like a normal
The following content is compiled by Yang Guo under the bodhi tree according to Microsoft msdn. Please indicate the source of the post. Msdn official explanation of func :Encapsulate a method that has a parameter and returns the type value specified
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.