功能/动作代表们引用参数/参数或匿名函数 [英] Func / Action delegates with reference arguments/parameters or anonymous functions
问题描述
我只是想知道,确切的语法是如何为 REF
和退出
参数的代表和内联lambda函数。
下面是一个例子。
如果一个函数被定义为
公共无效DoSomething的(INT withValue){}
函数中的委托可以通过
创建
公共无效F()
{
动作< ; INT> F2 = DoSomething的;
F2(3);
}
如何是语法,如果原来的功能将被定义为对>
公共无效DoSomething的(参考INT withValue){withValue = 3; }
您需要定义一个新的委托类型,这方法签名:
委托无效RefAction<在T>(REF牛逼OBJ);
公共无效F()
{
RefAction< INT> F2 = DoSomething的;
INT X = 0;
F2(REF X);
}
为什么.NET Framework中不包括该类型的原因,可能是因为 REF
参数不是很常见,如果你为每个可能的组合,添加一个委托类型需要的类型的数量爆炸。
I just wondered, how the exact syntax is for ref
and out
parameters for delegates and inline lambda functions.
here is an example
if a function is defined as
public void DoSomething(int withValue) { }
a delegate in a function can be created by
public void f()
{
Action<int> f2 = DoSomething;
f2(3);
}
how is that syntax, if the original function would be defined as
public void DoSomething(ref int withValue) { withValue = 3; }
You need to define a new delegate type for this method signature:
delegate void RefAction<in T>(ref T obj);
public void F()
{
RefAction<int> f2 = DoSomething;
int x = 0;
f2(ref x);
}
The reason why the .NET Framework does not include this type is probably because ref
parameters are not very common, and the number of needed types explodes if you add one delegate type for each possible combination.
这篇关于功能/动作代表们引用参数/参数或匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!