一般我们可以使用ref,out达到向外传递参数目的。 Action<T>是一个特殊的委托,除了常规应用。我们还可以用它来实现简单地向外传递参数。直接看下面的UnitTest代码:
1: [TestMethod]
void PassOutParametersUsingDelegate()
3: {
int i = 0;
string.Empty;
int? addedNumber = 1;
7:
//It is pass parameter by Action<T1,T2>
9: Foo1((a, b) =>
10: {
11: i = a;
12: messgae = b;
13: });
14:
//It is pass parameter by Func<T1,T2,TResult>
16: Foo2((a, b) =>
17: {
18: i = a;
19: messgae = b;
return addedNumber;
21: });
22:
23: Foo2((a, b) =>
24: {
return a+100;
26: });
27:
, i, messgae));
29:
30:
31: }
32:
string> action)
34: {
int number = 15;
;
37:
38: action(number, msg);
39: }
40:
int?> func)
42: {
int number = 15;
;
45:
int? tempInt = func(number, msg);
47: }