我可以有一个动作<>或FUNC。<>带出参数? [英] Can I have an Action<> or Func<> with an out param?

查看:132
本文介绍了我可以有一个动作<>或FUNC。<>带出参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用退出参数的方法,我想指出一个动作或<$ 。C $ C>函数功能(或其他类型的委托),它

I have a method with an out parameter, and I'd like to point an Action or Func (or other kind of delegate) at it.

这工作得很好:

static void Func(int a, int b) { }
Action<int,int> action = Func;



尽管如此,这并不

However this doesn't

static void OutFunc(out int a, out int b) { a = b = 0; }
Action<out int, out int> action = OutFunc; // loads of compile errors

这可能是重复的,但搜索出参数'ISN尤其不丰硕的成果。

This is probably a duplicate, but searching for 'out parameter' isn't particularly fruitful.

推荐答案

Action和Func键特别不拿出或ref参数。然而,他们只是委托。

Action and Func specifically do not take out or ref parameters. However, they are just delegates.

您可以使确实需要out参数自定义的委托类型,并使用它,但。

You can make a custom delegate type that does take an out parameter, and use it, though.

例如,下面的工作:

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();
    }
}

这打印出来:

3/5
0/0

这篇关于我可以有一个动作&LT;&GT;或FUNC。&LT;&GT;带出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆