【问题标题】:How to call other class method with out parameter如何使用out参数调用其他类方法
【发布时间】:2014-05-28 04:01:46
【问题描述】:

为什么不能用out参数调用另一个类的方法?例如:

class Program
{
    static void Main(string[] args)
    {
        int i =10;
        int j = OtherClass.Test(i);
    }
}

class OtherClass
{
    public static int Test(out int i)
    {
        i = 30;
        return i+15;
    }
}

我得到“'ConsoleApplication2.OtherClass.Test(out int)' 的最佳重载方法匹配有一些无效参数”错误??
我怎样才能做到这一点?我需要调用一些带有一些输出参数的通用静态方法。

谢谢

【问题讨论】:

  • OtherClass.Test(out i); 下次请先用google或msdn
  • 天哪!!!,算了,谢谢,我在一些复杂的实现之间搞糊涂了,忘记了一个简单的要求,再次感谢

标签: c# out


【解决方案1】:

调用者程序如下:

 class Program
 {
    static void Main(string[] args)
    {
       int i;
       int j = OtherClass.Test( out i);
    }
 }

【讨论】:

    【解决方案2】:

    你需要这样调用你的方法:

    int j = OtherClass.Test(out i);
    

    您还应该了解,通过这样做,您将更改作为参数传递给测试方法的“i”变量的值。

    更具体地说:执行 Test 方法后,'i' 的值将设置为 30。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 2020-07-29
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多