I. [Action <>] specifies the delegates with only input parameters and no return value.
1.1 define a delegate:
For example, the original write delegate:
Public Delegate void mydelegate (string Str); this delegate indicates that it can only delegate methods with only one input value but no return value, such as the following method.
1.2 definition method
Public static void hellowchinese (string strchinese)
{
Console. writeline ("good morning," + strchinese );
}
1.3 call
Mydelegate d = new mydelegate (hellowchinese );
D ("Wang Zong ");
1.4 Use action to delegate this method.
If we use the original method to call the hellowchinese method, it is very troublesome and we have to define a delegate. Do you need to clearly define the delegation?
All right, Microsoft has implemented the authorization of action. We only need to pass the methods that meet its requirements into it ..
I still use this method:
Public static void hellowchinese (string strchinese)
{
Console. writeline ("good morning," + strchinese );
}
When I call this method, I do not need to customize the delegate. In this way, you can:
Action <string> action = hellowchinese;
Action ("Wang Zong ");
This saves time and reduces many steps.
II. Func <> This is the same as the one above. The difference is that there is a returned value !~~~
2.1 previously we defined delegate
Public Delegate string mydelegate (string Str );
2. Definition Method
Public static string hellowenglish (string strenglish)
{
Return "Hellow" + strenglish;
}
2.3 call
Mydelegate d = new mydelegate (hellowenglish );
D ("Lanny ");
2.4 use func to call
Same Method
Public static string hellowenglish (string strenglish)
{
Return "Hellow" + strenglish;
}
But we do not need to customize delegate .. As follows:
Func <string, string> F = hellowenglish;
F ("Lanny ");
You can.
2.5
Differences between func and Resource Sharing