The difference between Action and Func in C
- 2020-10-23 21:11:37
- OfStack
This article analyzes the differences between Action and Func in C#, which will help readers to grasp it firmly and use it accurately. The specific analysis is as follows:
Let's start with this code:
// The common values used by the tests
int num = 10;
// test Func entrust
Func<int, int> f;
f = (int tempf) => { return tempf + 1; };
Response.Write(f(num).ToString()+"<br />"); // call f Delegate and print the corresponding value!
// test Action entrust
Action<int> a;
a = (int tempa) => { Response.Write(string.Format(" I can't return a value, so I have to print it here ! Your input parameter is : {0}", tempa)); };
a(num); // call a The delegate
Main Differences:
Func
<
t, Result
>
Encapsulates a method that takes 1 parameter and returns the type value specified by the TResult parameter.
Action
<
t
>
Encapsulates a method that takes only one parameter and returns no value.
I hope this article has helped you with your C# programming.