我有一个带out参数的方法,我想指出一个Action或Func(或其他类型的委托).
这很好用:
static void Func(int a, int b) { }
Action<int,int> action = Func;
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
static void OutFunc(out int a, out int b) { a = b = 0; }
Action<out int, out int> action = OutFunc; // loads of compile errors
Run Code Online (Sandbox Code Playgroud)
这可能是重复的,但搜索'out参数'并不是特别富有成效.
Ree*_*sey 52
Action和Func特别不取出或参数.但是,他们只是代表.
但是,您可以创建一个自定义委托类型,该类型确实采用out参数,并使用它.
例如,以下工作:
class Program
{
static void OutFunc(out int a, out int b) { a = b = 0; }
public delegate void OutAction<T1,T2>(out T1 a, out T2 b);
static void Main(string[] args)
{
OutAction<int, int> action = OutFunc;
int a = 3, b = 5;
Console.WriteLine("{0}/{1}",a,b);
action(out a, out b);
Console.WriteLine("{0}/{1}", a, b);
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
打印出:
3/5
0/0
Run Code Online (Sandbox Code Playgroud)