是否有可能使用参考类型C#内置动作<>代表? [英] Is it possible to use ref types in C# built-in Action<> delegate?
问题描述
C#还内置了代表动作<>
和 Func键<>
。是否有可能使用这个代表裁判类型参数?例如,下面的代码:
C# has built-in delegates Action<>
and Func<>
. Is it possible to use 'ref' type parameters for this delegates? For example, this code:
public delegate void DTest( ref Guid a );
public event DTest ETest;
将编译。但是,如果使用动作<>
,将不能编译:
public event Action< ref Guid > ETest;
任何提示?
Any hints?
推荐答案
没有,你不能使用传址引用与动作
代表。虽然有类型按引用传递作为一个概念的键入
在框架中,它不是真正在正常意义上的类型尽可能C#而言。 REF
是该参数的修改,而不是类型名称的一部分,如果你明白我的意思。
No, you can't use pass-by-reference with the Action
delegates. While there is a concept of "type passed by reference" as a Type
in the framework, it's not really a type in the normal sense as far as C# is concerned. ref
is a modifier for the parameter, not part of the type name, if you see what I mean.
但是,你可以建立自己的一套等同类型,如:
However, you can build your own set of equivalent types, e.g.
delegate void ActionRef<T>(ref T item);
当然,如果你想要的参考和非ref参数混合在相同的委托,你进入一个可怕的一套组合:
Of course, if you want a mixture of ref and non-ref parameters in the same delegate, you get into a horrible set of combinations:
delegate void ActionRef1<T1, T2>(ref T1 arg1, T2 arg2);
delegate void ActionRef2<T1, T2>(T1 arg1, ref T2 arg2);
delegate void ActionRef3<T1, T2>(ref T1 arg1, ref T2 arg2);
这篇关于是否有可能使用参考类型C#内置动作<>代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!