I. Func
Func is a. NET built-in delegate.
Func<result>,func<t1,result> is a. NET built-in generic delegates.
- Func<tresult>
- Func<t,tresult>
- Func<t1,t2,tresult>
- Func<t1,t2,t3,tresult>
- Func<t1,t2,t3,t4,tresult>
It has 5 forms, except that the number of parameters is different; the first one is no parameter, but there is a return value;
The following is an example of a simple generic delegate to pass a method.
Private delegate string Say (); public static string SayHello () { return "Hello"; } static void Main (string[] args) { Say Say = SayHello; Console.WriteLine (Say ()); Console.readkey (); }
So, at times, when we don't know what an interface is going to do at the same time, I can leave a delegate to it.
For more convenience,. NET has a delegate directly by default. Let's try it again. NET default with a delegate.
public static string SayHello () { return "Hello"; } static void Main (string[] args) { func<string> say = SayHello; Console.WriteLine (Say ()); Console.readkey (); }
If you need a parameter, you can also send a copy of it.
public static string SayHello (String str) { return str + str; } static void Main (string[] args) { func<string, string> say = SayHello; String str = Say ("abc"); Console.WriteLine (str); Output ABCABC console.readkey (); }
Second, Action
The use of action<t> is almost the same as Func, and the invocation method is similar.
- Action
- Action<t>
- Action<t1,t2>
- Action<t1,t2,t3>
- Action<t1,t2,t3,t4>
Private delegate string Say (); public static void SayHello (String str) { Console.WriteLine (str); } static void Main (string[] args) { action<string> say = SayHello; Say ("abc"); Console.readkey (); }
iii. The difference between Func and action
Func is almost the same as action. It's just that
- Func<result> has a return type;
- Action<t> only the parameter type, it cannot be passed the return type. So action<t> 's delegate function has no return value.
Iv. Func and action support the form of lambda invocation
Or, after one input, returns the value that repeats once as an example.
Func<string, string> say = m + = m + M; Console.WriteLine (Say ("abc")); Output ABCABC
Func<t> and action<t> are different from the description